diff --git a/main.py b/main.py index 0c06d63..99ea6de 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,7 @@ import os # Filesystem import mimetypes # Uploading files to homeserver import html # HTML formatting import requests # XKCD command -import subprocess # ping command (admin) +import subprocess # GIF Emojis, ping command (admin) import random # roll command import datetime # Ratelimiting @@ -87,13 +87,24 @@ def download_file(url, filename): else: return False +# GIF resizing +def thumbnails(frames, size): + for frame in frames: + thumbnail = frame.copy() + thumbnail.thumbnail((size, size), Image.ANTIALIAS) + yield thumbnail + # Resize an image (squares only) def resize_image(filename, size): - im = Image.open(filename) - im = im.resize((size,size)) - # Remove .ext from the filename, insert _resized into it, and save it + original_filename = filename filename = filename[:-4] + "_resized" + filename[-4:] - im.save(filename) + if filename.endswith(".gif"): + # PIL does not properly hanle GIF images + subprocess.run(["gifsicle", "--resize", f"{str(size)}x{str(size)}", original_filename, "-o", filename], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + else: + im = Image.open(original_filename) + im = im.resize((size,size)) + im.save(filename) return filename # Mentions a user