mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-26 17:24:31 +03:00
Merge pull request #765 from larsjsol/master
Fix dispose calculations for animated GIFs
This commit is contained in:
commit
cb5ed5973a
|
@ -96,8 +96,15 @@ class GifImageFile(ImageFile.ImageFile):
|
|||
# rewind
|
||||
self.__offset = 0
|
||||
self.dispose = None
|
||||
self.dispose_extent = [0, 0, 0, 0] #x0, y0, x1, y1
|
||||
self.__frame = -1
|
||||
self.__fp.seek(self.__rewind)
|
||||
self._prev_im = None
|
||||
self.disposal_method = 0
|
||||
else:
|
||||
# ensure that the previous frame was loaded
|
||||
if not self.im:
|
||||
self.load()
|
||||
|
||||
if frame != self.__frame + 1:
|
||||
raise ValueError("cannot seek to frame %d" % frame)
|
||||
|
@ -114,8 +121,7 @@ class GifImageFile(ImageFile.ImageFile):
|
|||
self.__offset = 0
|
||||
|
||||
if self.dispose:
|
||||
self.im = self.dispose
|
||||
self.dispose = None
|
||||
self.im.paste(self.dispose, self.dispose_extent)
|
||||
|
||||
from copy import copy
|
||||
self.palette = copy(self.global_palette)
|
||||
|
@ -140,17 +146,16 @@ class GifImageFile(ImageFile.ImageFile):
|
|||
if flags & 1:
|
||||
self.info["transparency"] = i8(block[3])
|
||||
self.info["duration"] = i16(block[1:3]) * 10
|
||||
try:
|
||||
# disposal methods
|
||||
if flags & 8:
|
||||
# replace with background colour
|
||||
self.dispose = Image.core.fill("P", self.size,
|
||||
self.info["background"])
|
||||
elif flags & 16:
|
||||
# replace with previous contents
|
||||
self.dispose = self.im.copy()
|
||||
except (AttributeError, KeyError):
|
||||
pass
|
||||
|
||||
# disposal method - find the value of bits 4 - 6
|
||||
dispose_bits = 0b00011100 & flags
|
||||
dispose_bits = dispose_bits >> 2
|
||||
if dispose_bits:
|
||||
# only set the dispose if it is not
|
||||
# unspecified. I'm not sure if this is
|
||||
# correct, but it seems to prevent the last
|
||||
# frame from looking odd for some animations
|
||||
self.disposal_method = dispose_bits
|
||||
elif i8(s) == 255:
|
||||
#
|
||||
# application extension
|
||||
|
@ -172,6 +177,7 @@ class GifImageFile(ImageFile.ImageFile):
|
|||
# extent
|
||||
x0, y0 = i16(s[0:]), i16(s[2:])
|
||||
x1, y1 = x0 + i16(s[4:]), y0 + i16(s[6:])
|
||||
self.dispose_extent = x0, y0, x1, y1
|
||||
flags = i8(s[8])
|
||||
|
||||
interlace = (flags & 64) != 0
|
||||
|
@ -194,6 +200,26 @@ class GifImageFile(ImageFile.ImageFile):
|
|||
pass
|
||||
# raise IOError, "illegal GIF tag `%x`" % i8(s)
|
||||
|
||||
try:
|
||||
if self.disposal_method < 2:
|
||||
# do not dispose or none specified
|
||||
self.dispose = None
|
||||
elif self.disposal_method == 2:
|
||||
# replace with background colour
|
||||
self.dispose = Image.core.fill("P", self.size,
|
||||
self.info["background"])
|
||||
else:
|
||||
# replace with previous contents
|
||||
if self.im:
|
||||
self.dispose = self.im.copy()
|
||||
|
||||
# only dispose the extent in this frame
|
||||
if self.dispose:
|
||||
self.dispose = self.dispose.crop(self.dispose_extent)
|
||||
except (AttributeError, KeyError):
|
||||
pass
|
||||
|
||||
|
||||
if not self.tile:
|
||||
# self.__fp = None
|
||||
raise EOFError("no more images in GIF file")
|
||||
|
@ -205,6 +231,18 @@ class GifImageFile(ImageFile.ImageFile):
|
|||
def tell(self):
|
||||
return self.__frame
|
||||
|
||||
def load_end(self):
|
||||
ImageFile.ImageFile.load_end(self)
|
||||
|
||||
# if the disposal method is 'do not dispose', transparent
|
||||
# pixels should show the content of the previous frame
|
||||
if self._prev_im and self.disposal_method == 1:
|
||||
# we do this by pasting the updated area onto the previous
|
||||
# frame which we then use as the current image content
|
||||
updated = self.im.crop(self.dispose_extent)
|
||||
self._prev_im.paste(updated, self.dispose_extent, updated.convert('RGBA'))
|
||||
self.im = self._prev_im
|
||||
self._prev_im = self.im.copy()
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# Write GIF files
|
||||
|
|
BIN
Tests/images/dispose_bgnd.gif
Normal file
BIN
Tests/images/dispose_bgnd.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
BIN
Tests/images/dispose_none.gif
Normal file
BIN
Tests/images/dispose_none.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
BIN
Tests/images/dispose_prev.gif
Normal file
BIN
Tests/images/dispose_prev.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
BIN
Tests/images/iss634.gif
Normal file
BIN
Tests/images/iss634.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 271 KiB |
|
@ -106,6 +106,50 @@ class TestFileGif(PillowTestCase):
|
|||
GifImagePlugin._save_netpbm(img, 0, tempfile)
|
||||
self.assert_image_similar(img, Image.open(tempfile).convert("L"), 0)
|
||||
|
||||
def test_seek(self):
|
||||
img = Image.open("Tests/images/dispose_none.gif")
|
||||
framecount = 0
|
||||
try:
|
||||
while True:
|
||||
framecount += 1
|
||||
img.seek(img.tell() +1)
|
||||
except EOFError:
|
||||
self.assertEqual(framecount, 5)
|
||||
|
||||
def test_dispose_none(self):
|
||||
img = Image.open("Tests/images/dispose_none.gif")
|
||||
try:
|
||||
while True:
|
||||
img.seek(img.tell() +1)
|
||||
self.assertEqual(img.disposal_method, 1)
|
||||
except EOFError:
|
||||
pass
|
||||
|
||||
def test_dispose_background(self):
|
||||
img = Image.open("Tests/images/dispose_bgnd.gif")
|
||||
try:
|
||||
while True:
|
||||
img.seek(img.tell() +1)
|
||||
self.assertEqual(img.disposal_method, 2)
|
||||
except EOFError:
|
||||
pass
|
||||
|
||||
def test_dispose_previous(self):
|
||||
img = Image.open("Tests/images/dispose_prev.gif")
|
||||
try:
|
||||
while True:
|
||||
img.seek(img.tell() +1)
|
||||
self.assertEqual(img.disposal_method, 3)
|
||||
except EOFError:
|
||||
pass
|
||||
|
||||
def test_iss634(self):
|
||||
img = Image.open("Tests/images/iss634.gif")
|
||||
# seek to the second frame
|
||||
img.seek(img.tell() +1)
|
||||
# all transparent pixels should be replaced with the color from the first frame
|
||||
self.assertEqual(img.histogram()[img.info['transparency']], 0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
Loading…
Reference in New Issue
Block a user