mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 17:36:18 +03:00
Merge pull request #1231 from radarhere/image_palette
Copy image when saving in GifImagePlugin
This commit is contained in:
commit
30631902d6
|
@ -290,7 +290,7 @@ def _save(im, fp, filename):
|
|||
pass # write uncompressed file
|
||||
|
||||
if im.mode in RAWMODE:
|
||||
im_out = im
|
||||
im_out = im.copy()
|
||||
else:
|
||||
# convert on the fly (EXPERIMENTAL -- I'm not sure PIL
|
||||
# should automatically convert images on save...)
|
||||
|
|
|
@ -509,7 +509,8 @@ class Image(object):
|
|||
new.im = im
|
||||
new.mode = im.mode
|
||||
new.size = im.size
|
||||
new.palette = self.palette
|
||||
if self.palette:
|
||||
new.palette = self.palette.copy()
|
||||
if im.mode == "P" and not new.palette:
|
||||
from PIL import ImagePalette
|
||||
new.palette = ImagePalette.ImagePalette()
|
||||
|
|
|
@ -34,6 +34,18 @@ class ImagePalette(object):
|
|||
(size != 0 and size != len(self.palette))):
|
||||
raise ValueError("wrong palette size")
|
||||
|
||||
def copy(self):
|
||||
new = ImagePalette()
|
||||
|
||||
new.mode = self.mode
|
||||
new.rawmode = self.rawmode
|
||||
if self.palette is not None:
|
||||
new.palette = self.palette[:]
|
||||
new.colors = self.colors.copy()
|
||||
new.dirty = self.dirty
|
||||
|
||||
return new
|
||||
|
||||
def getdata(self):
|
||||
"""
|
||||
Get palette contents in format suitable # for the low-level
|
||||
|
|
|
@ -92,20 +92,20 @@ class TestFileGif(PillowTestCase):
|
|||
|
||||
def roundtrip(im, *args, **kwargs):
|
||||
out = self.tempfile('temp.gif')
|
||||
im.save(out, *args, **kwargs)
|
||||
im.copy().save(out, *args, **kwargs)
|
||||
reloaded = Image.open(out)
|
||||
|
||||
return [im, reloaded]
|
||||
return reloaded
|
||||
|
||||
orig = "Tests/images/test.colors.gif"
|
||||
im = Image.open(orig)
|
||||
|
||||
self.assert_image_equal(*roundtrip(im))
|
||||
self.assert_image_equal(*roundtrip(im, optimize=True))
|
||||
self.assert_image_similar(im, roundtrip(im), 1)
|
||||
self.assert_image_similar(im, roundtrip(im, optimize=True), 1)
|
||||
|
||||
im = im.convert("RGB")
|
||||
# check automatic P conversion
|
||||
reloaded = roundtrip(im)[1].convert('RGB')
|
||||
reloaded = roundtrip(im).convert('RGB')
|
||||
self.assert_image_equal(im, reloaded)
|
||||
|
||||
@unittest.skipUnless(netpbm_available(), "netpbm not available")
|
||||
|
|
|
@ -26,7 +26,7 @@ class TestImageFile(PillowTestCase):
|
|||
|
||||
test_file = BytesIO()
|
||||
|
||||
im.save(test_file, format)
|
||||
im.copy().save(test_file, format)
|
||||
|
||||
data = test_file.getvalue()
|
||||
|
||||
|
@ -37,7 +37,8 @@ class TestImageFile(PillowTestCase):
|
|||
return im, imOut
|
||||
|
||||
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("MSP"))
|
||||
if "zip_encoder" in codecs:
|
||||
|
|
Loading…
Reference in New Issue
Block a user