Skip to content

Commit

Permalink
emoji-upload: Fix transparency issues on GIF emoji upload.
Browse files Browse the repository at this point in the history
This preserves the alpha layer on GIF images that need to be resized
before being uploaded.  Two important changes occur here:

1. The new frame is a *copy* of the original image, which preserves the
   GIF info.
2. The disposal method of the original GIF is preserved.  This
   essentially determines what state each frame of the GIF starts from
   when it is drawn; see PIL's docs:
   https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#saving
   for more info.
  • Loading branch information
Cody Piersall committed Oct 2, 2020
1 parent 08fbde4 commit 47223d1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions zerver/lib/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,22 @@ def resize_logo(image_data: bytes) -> bytes:
def resize_gif(im: GifImageFile, size: int=DEFAULT_EMOJI_SIZE) -> bytes:
frames = []
duration_info = []
disposals = []
# If 'loop' info is not set then loop for infinite number of times.
loop = im.info.get("loop", 0)
for frame_num in range(0, im.n_frames):
im.seek(frame_num)
new_frame = Image.new("RGBA", im.size)
new_frame = im.copy()
new_frame.paste(im, (0, 0), im.convert("RGBA"))
new_frame = ImageOps.fit(new_frame, (size, size), Image.ANTIALIAS)
frames.append(new_frame)
duration_info.append(im.info['duration'])
disposals.append(im.disposal_method)
out = io.BytesIO()
frames[0].save(out, save_all=True, optimize=True,
frames[0].save(out, save_all=True, optimize=False,
format="GIF", append_images=frames[1:],
duration=duration_info,
disposal=disposals,
loop=loop)
return out.getvalue()

Expand Down

0 comments on commit 47223d1

Please sign in to comment.