forked from rDrama/rDrama
1
0
Fork 0
retarD/files/helpers/images.py

38 lines
1.0 KiB
Python
Raw Normal View History

2022-01-25 00:33:16 +00:00
from PIL import Image, ImageSequence, ImageOps
2021-10-15 14:08:27 +00:00
from webptools import gifwebp
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-01-24 18:11: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-23 17:46:28 +00:00
2021-10-15 14:08:27 +00:00
if resize:
2022-01-25 00:33:16 +00:00
size = resize, resize
frames = ImageSequence.Iterator(i)
def thumbnails(frames):
for frame in frames:
thumbnail = frame.copy()
thumbnail.thumbnail(size)
yield thumbnail
frames = thumbnails(frames)
om = next(frames)
om.info = i.info
om = ImageOps.exif_transpose(om)
om.save(filename, format="WEBP", save_all=True, append_images=list(frames), loop=0, method=6, allow_mixed=True)
2021-10-15 14:08:27 +00:00
elif i.format.lower() != "webp":
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}'