From 1cf978ad226dadecf41e6b913d6284ebde7a2a2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Mon, 7 May 2018 19:54:36 +0200 Subject: [PATCH] [preview] Fix speed of animated GIFs --- url_image_preview/resize_gif.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/url_image_preview/resize_gif.py b/url_image_preview/resize_gif.py index c5f0abd..1147269 100644 --- a/url_image_preview/resize_gif.py +++ b/url_image_preview/resize_gif.py @@ -3,7 +3,7 @@ from PIL import Image def resize_gif(mem, path, resize_to): - frames = extract_and_resize_frames(mem, resize_to) + frames, result = extract_and_resize_frames(mem, resize_to) if len(frames) == 1: frames[0].save(path, optimize=True) @@ -12,6 +12,7 @@ def resize_gif(mem, path, resize_to): optimize=True, save_all=True, append_images=frames[1:], + duration=result['duration'], loop=1000) @@ -25,7 +26,9 @@ def analyse_image(mem): results = { 'size': image.size, 'mode': 'full', + 'duration': image.info["duration"] } + try: while True: if image.tile: @@ -42,7 +45,7 @@ def analyse_image(mem): def extract_and_resize_frames(mem, resize_to): - mode = analyse_image(mem)['mode'] + result = analyse_image(mem) image = Image.open(BytesIO(mem)) i = 0 @@ -69,7 +72,7 @@ def extract_and_resize_frames(mem, resize_to): If so, we need to construct the new frame by pasting it on top of the preceding frames. ''' - if mode == 'partial': + if result['mode'] == 'partial': new_frame.paste(last_frame) new_frame.paste(image, (0, 0), image.convert('RGBA')) @@ -84,4 +87,4 @@ def extract_and_resize_frames(mem, resize_to): except EOFError: pass - return frames + return frames, result