To avoid modifications, copy image when saving in GifImagePlugin

This commit is contained in:
Andrew Murray 2015-05-13 16:39:25 +10:00
parent 8f0dca2333
commit ca89d431ba
3 changed files with 9 additions and 8 deletions

View File

@ -269,7 +269,7 @@ def _save(im, fp, filename):
pass # write uncompressed file pass # write uncompressed file
if im.mode in RAWMODE: if im.mode in RAWMODE:
im_out = im im_out = im.copy()
else: else:
# convert on the fly (EXPERIMENTAL -- I'm not sure PIL # convert on the fly (EXPERIMENTAL -- I'm not sure PIL
# should automatically convert images on save...) # should automatically convert images on save...)

View File

@ -92,20 +92,20 @@ class TestFileGif(PillowTestCase):
def roundtrip(im, *args, **kwargs): def roundtrip(im, *args, **kwargs):
out = self.tempfile('temp.gif') out = self.tempfile('temp.gif')
im.save(out, *args, **kwargs) im.copy().save(out, *args, **kwargs)
reloaded = Image.open(out) reloaded = Image.open(out)
return [im, reloaded] return reloaded
orig = "Tests/images/test.colors.gif" orig = "Tests/images/test.colors.gif"
im = Image.open(orig) im = Image.open(orig)
self.assert_image_equal(*roundtrip(im)) self.assert_image_similar(im, roundtrip(im), 1)
self.assert_image_equal(*roundtrip(im, optimize=True)) self.assert_image_similar(im, roundtrip(im, optimize=True), 1)
im = im.convert("RGB") im = im.convert("RGB")
# check automatic P conversion # check automatic P conversion
reloaded = roundtrip(im)[1].convert('RGB') reloaded = roundtrip(im).convert('RGB')
self.assert_image_equal(im, reloaded) self.assert_image_equal(im, reloaded)
@unittest.skipUnless(netpbm_available(), "netpbm not available") @unittest.skipUnless(netpbm_available(), "netpbm not available")

View File

@ -26,7 +26,7 @@ class TestImageFile(PillowTestCase):
test_file = BytesIO() test_file = BytesIO()
im.save(test_file, format) im.copy().save(test_file, format)
data = test_file.getvalue() data = test_file.getvalue()
@ -37,7 +37,8 @@ class TestImageFile(PillowTestCase):
return im, imOut return im, imOut
self.assert_image_equal(*roundtrip("BMP")) self.assert_image_equal(*roundtrip("BMP"))
self.assert_image_equal(*roundtrip("GIF")) im1, im2 = roundtrip("GIF")
self.assert_image_similar(im1.convert('P'), im2, 1)
self.assert_image_equal(*roundtrip("IM")) self.assert_image_equal(*roundtrip("IM"))
self.assert_image_equal(*roundtrip("MSP")) self.assert_image_equal(*roundtrip("MSP"))
if "zip_encoder" in codecs: if "zip_encoder" in codecs: