Create background image for calculating gif deltas

This commit is contained in:
Riley Lahd 2019-03-11 07:41:14 -06:00
parent 4a2be2af2d
commit 3b1a1fbfd2

View File

@ -443,20 +443,20 @@ def _write_multiple_frames(im, fp, palette):
if im_frames: if im_frames:
# delta frame # delta frame
previous = im_frames[-1] previous = im_frames[-1]
if encoderinfo["disposal"] == 2: if ("disposal" in im.encoderinfo and im.encoderinfo["disposal"] == 2):
# Entire frame should be delta base_image = background
# Create delta by subtracting empty image from frame (This is required) else:
delta = ImageChops.subtract_modulo( base_image = previous["im"]
im_frame, Image.new('P', (0,0)))
elif _get_palette_bytes(im_frame) == \ if _get_palette_bytes(im_frame) == \
_get_palette_bytes(previous['im']): _get_palette_bytes(base_image):
delta = ImageChops.subtract_modulo(im_frame, delta = ImageChops.subtract_modulo(im_frame,
previous['im']) base_image)
else: else:
delta = ImageChops.subtract_modulo( delta = ImageChops.subtract_modulo(
im_frame.convert('RGB'), previous['im'].convert('RGB')) im_frame.convert('RGB'), base_image.convert('RGB'))
bbox = delta.getbbox() bbox = delta.getbbox()
if not bbox: if not bbox and not ("disposal" in im.encoderinfo and im.encoderinfo["disposal"] == 2):
# This frame is identical to the previous frame # This frame is identical to the previous frame
if duration: if duration:
previous['encoderinfo']['duration'] += \ previous['encoderinfo']['duration'] += \
@ -464,6 +464,7 @@ def _write_multiple_frames(im, fp, palette):
continue continue
else: else:
bbox = None bbox = None
background = Image.new("P", im_frame.size, 0)
im_frames.append({ im_frames.append({
'im': im_frame, 'im': im_frame,
'bbox': bbox, 'bbox': bbox,