rDrama/files/helpers/images.py

27 lines
702 B
Python
Raw Normal View History

2021-09-15 00:46:08 +00:00
from PIL import Image as IImage, ImageSequence
2021-09-15 09:15:56 +00:00
from webptools import gifwebp
2021-07-21 01:12:26 +00:00
2021-10-03 19:05:59 +00:00
def process_image(filename=None, resize=False):
2021-09-13 16:35:26 +00:00
2021-10-03 19:05:59 +00:00
i = IImage.open(filename)
2021-09-15 09:29:07 +00:00
2021-09-13 17:01:55 +00:00
if resize:
size = 100, 100
frames = ImageSequence.Iterator(i)
2021-09-13 16:56:22 +00:00
2021-09-13 17:01:55 +00:00
def thumbnails(frames):
for frame in frames:
2021-09-13 16:58:15 +00:00
thumbnail = frame.copy()
2021-09-15 00:46:25 +00:00
thumbnail.thumbnail(size)
2021-09-13 16:58:15 +00:00
yield thumbnail
2021-09-13 17:01:55 +00:00
frames = thumbnails(frames)
2021-09-13 16:56:22 +00:00
2021-09-13 17:01:55 +00:00
om = next(frames)
om.info = i.info
2021-10-03 21:51:39 +00:00
om.save(filename, format="WEBP", save_all=True, append_images=list(frames), loop=0)
2021-09-23 16:47:48 +00:00
elif i.format.lower() != "webp":
2021-10-03 19:05:59 +00:00
if i.format.lower() == "gif": gifwebp(input_image=filename, output_image=filename, option="-q 80")
2021-10-03 21:51:39 +00:00
else: i.save(filename, format="WEBP")
2021-09-13 16:35:26 +00:00
2021-10-03 19:05:59 +00:00
return filename