[preview] Fix speed of animated GIFs

This commit is contained in:
Philipp Hörist
2018-05-07 19:54:36 +02:00
parent 96ed448a54
commit 1cf978ad22

View File

@@ -3,7 +3,7 @@ from PIL import Image
def resize_gif(mem, path, resize_to): 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: if len(frames) == 1:
frames[0].save(path, optimize=True) frames[0].save(path, optimize=True)
@@ -12,6 +12,7 @@ def resize_gif(mem, path, resize_to):
optimize=True, optimize=True,
save_all=True, save_all=True,
append_images=frames[1:], append_images=frames[1:],
duration=result['duration'],
loop=1000) loop=1000)
@@ -25,7 +26,9 @@ def analyse_image(mem):
results = { results = {
'size': image.size, 'size': image.size,
'mode': 'full', 'mode': 'full',
'duration': image.info["duration"]
} }
try: try:
while True: while True:
if image.tile: if image.tile:
@@ -42,7 +45,7 @@ def analyse_image(mem):
def extract_and_resize_frames(mem, resize_to): def extract_and_resize_frames(mem, resize_to):
mode = analyse_image(mem)['mode'] result = analyse_image(mem)
image = Image.open(BytesIO(mem)) image = Image.open(BytesIO(mem))
i = 0 i = 0
@@ -69,7 +72,7 @@ def extract_and_resize_frames(mem, resize_to):
If so, we need to construct the new frame by If so, we need to construct the new frame by
pasting it on top of the preceding frames. pasting it on top of the preceding frames.
''' '''
if mode == 'partial': if result['mode'] == 'partial':
new_frame.paste(last_frame) new_frame.paste(last_frame)
new_frame.paste(image, (0, 0), image.convert('RGBA')) new_frame.paste(image, (0, 0), image.convert('RGBA'))
@@ -84,4 +87,4 @@ def extract_and_resize_frames(mem, resize_to):
except EOFError: except EOFError:
pass pass
return frames return frames, result