2022-02-16 22:23:44 +00:00
|
|
|
from PIL import Image, ImageOps
|
|
|
|
from PIL.ImageSequence import Iterator
|
2021-10-15 14:08:27 +00:00
|
|
|
from webptools import gifwebp
|
2022-02-16 22:23:44 +00:00
|
|
|
import subprocess
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-01-24 23:40:34 +00:00
|
|
|
def process_image(filename=None, resize=0):
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-01-24 23:40:34 +00:00
|
|
|
i = Image.open(filename)
|
2022-01-21 23:37:56 +00:00
|
|
|
|
2022-02-03 06:39:02 +00:00
|
|
|
if resize and i.width > resize:
|
2022-02-16 22:23:44 +00:00
|
|
|
subprocess.call(["convert", filename, "-coalesce", "-resize", f"{resize}>", filename])
|
|
|
|
elif i.format.lower() != "webp":
|
2022-01-25 00:33:16 +00:00
|
|
|
|
2022-02-16 22:23:44 +00:00
|
|
|
exif = i.getexif()
|
|
|
|
for k in exif.keys():
|
|
|
|
if k != 0x0112:
|
|
|
|
exif[k] = None
|
|
|
|
del exif[k]
|
|
|
|
i.info["exif"] = exif.tobytes()
|
2022-01-25 00:33:16 +00:00
|
|
|
|
2022-01-21 19:55:42 +00:00
|
|
|
if i.format.lower() == "gif":
|
|
|
|
gifwebp(input_image=filename, output_image=filename, option="-mixed -metadata none -f 100 -mt -m 6")
|
2022-01-24 18:07:59 +00:00
|
|
|
else:
|
|
|
|
i = ImageOps.exif_transpose(i)
|
|
|
|
i.save(filename, format="WEBP", method=6)
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-12-26 04:03:58 +00:00
|
|
|
return f'/static{filename}'
|