Compare commits

...

2 Commits

Author SHA1 Message Date
xz-dev 2aa227312d
Merge aa8616a40a into 333567f481 2024-09-16 20:43:28 +08:00
xz-dev aa8616a40a
perf: improved performance of webp_to_others 2024-09-16 20:08:05 +08:00
1 changed files with 11 additions and 13 deletions

View File

@ -120,28 +120,26 @@ def process_frame(frame):
def webp_to_others(data: bytes, mimetype: str) -> bytes: def webp_to_others(data: bytes, mimetype: str) -> bytes:
with tempfile.NamedTemporaryFile(suffix=".webp") as webp: format = mimetypes.guess_extension(mimetype)[1:]
webp.write(data) print(format)
webp.flush() with Image.open(BytesIO(data)) as webp:
ext = mimetypes.guess_extension(mimetype) with BytesIO() as img:
with tempfile.NamedTemporaryFile(suffix=ext) as img:
print(".", end="", flush=True) print(".", end="", flush=True)
im = Image.open(webp.name) webp.info.pop('background', None)
im.info.pop('background', None)
if mimetype == "image/gif": if mimetype == "image/gif":
frames = [] frames = []
duration = [] duration = [100, ]
for frame in ImageSequence.Iterator(im): for frame in ImageSequence.Iterator(webp):
frame = process_frame(frame) frame = process_frame(frame)
frames.append(frame) frames.append(frame)
duration.append(frame.info.get('duration', 100)) duration.append(frame.info.get('duration', duration[-1]))
frames[0].save(img.name, save_all=True, lossless=True, quality=100, method=6, frames[0].save(img, format=format, save_all=True, lossless=True, quality=100, method=6,
append_images=frames[1:], loop=0, duration=duration, disposal=2) append_images=frames[1:], loop=0, duration=duration[1:], disposal=2)
else: else:
im.save(img.name, save_all=True, lossless=True, quality=100, method=6) webp.save(img, format=format, lossless=True, quality=100, method=6)
img.seek(0) img.seek(0)
return img.read() return img.read()