MarseyWorld/files/helpers/images.py

28 lines
783 B
Python

from PIL import Image, ImageOps
from PIL.ImageSequence import Iterator
from webptools import gifwebp
import subprocess
def process_image(filename=None, resize=0):
i = Image.open(filename)
if resize and i.width > resize:
try: subprocess.call(["convert", filename, "-coalesce", "-resize", f"{resize}>", filename])
except: pass
elif i.format.lower() != "webp":
exif = i.getexif()
for k in exif.keys():
if k != 0x0112:
exif[k] = None
del exif[k]
i.info["exif"] = exif.tobytes()
if i.format.lower() == "gif":
gifwebp(input_image=filename, output_image=filename, option="-mixed -metadata none -f 100 -mt -m 6")
else:
i = ImageOps.exif_transpose(i)
i.save(filename, format="WEBP", method=6)
return f'/static{filename}'