Added multiframe GIF test

This commit is contained in:
Andrew Murray 2015-07-01 09:18:05 +10:00
parent 53d0e75d3f
commit 4fbea3e553
2 changed files with 27 additions and 10 deletions

View File

@ -299,9 +299,22 @@ RAWMODE = {
"P": "P", "P": "P",
} }
def _convert_mode(im):
# convert on the fly (EXPERIMENTAL -- I'm not sure PIL
# should automatically convert images on save...)
if Image.getmodebase(im.mode) == "RGB":
palette_size = 256
if im.palette:
palette_size = len(im.palette.getdata()[1]) // 3
return im.convert("P", palette=1, colors=palette_size)
return im.convert("L")
def _save_all(im, fp, filename): def _save_all(im, fp, filename):
_save(im, fp, filename, save_all=True) _save(im, fp, filename, save_all=True)
def _save(im, fp, filename, save_all=False): def _save(im, fp, filename, save_all=False):
if _imaging_gif: if _imaging_gif:
@ -315,15 +328,7 @@ def _save(im, fp, filename, save_all=False):
if im.mode in RAWMODE: if im.mode in RAWMODE:
im_out = im.copy() im_out = im.copy()
else: else:
# convert on the fly (EXPERIMENTAL -- I'm not sure PIL im_out = _convert_mode(im)
# should automatically convert images on save...)
if Image.getmodebase(im.mode) == "RGB":
palette_size = 256
if im.palette:
palette_size = len(im.palette.getdata()[1]) // 3
im_out = im.convert("P", palette=1, colors=palette_size)
else:
im_out = im.convert("L")
# header # header
try: try:
@ -335,7 +340,9 @@ def _save(im, fp, filename, save_all=False):
if save_all: if save_all:
previous = None previous = None
for im_frame in ImageSequence.Iterator(im_out): for im_frame in ImageSequence.Iterator(im):
im_frame = _convert_mode(im_frame)
# To specify duration, add the time in milliseconds to getdata(), # To specify duration, add the time in milliseconds to getdata(),
# e.g. getdata(im_frame, duration=1000) # e.g. getdata(im_frame, duration=1000)
if not previous: if not previous:

View File

@ -72,6 +72,7 @@ class TestFileGif(PillowTestCase):
self.assert_image_similar(reread.convert('RGB'), hopper(), 50) self.assert_image_similar(reread.convert('RGB'), hopper(), 50)
def test_roundtrip_save_all(self): def test_roundtrip_save_all(self):
# Single frame image
out = self.tempfile('temp.gif') out = self.tempfile('temp.gif')
im = hopper() im = hopper()
im.save(out, save_all=True) im.save(out, save_all=True)
@ -79,6 +80,15 @@ class TestFileGif(PillowTestCase):
self.assert_image_similar(reread.convert('RGB'), im, 50) self.assert_image_similar(reread.convert('RGB'), im, 50)
# Multiframe image
im = Image.open("Tests/images/dispose_bgnd.gif")
out = self.tempfile('temp.gif')
im.save(out, save_all=True)
reread = Image.open(out)
self.assertEqual(im.n_frames, 5)
def test_palette_handling(self): def test_palette_handling(self):
# see https://github.com/python-pillow/Pillow/issues/513 # see https://github.com/python-pillow/Pillow/issues/513