From b1e8a68df8e7c858c78b89681678396005a678dc Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 14 May 2015 09:57:56 +1000 Subject: [PATCH] When copying Image, copy ImagePalette as well --- PIL/Image.py | 3 ++- PIL/ImagePalette.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index 37428ec30..8ef4969ef 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -509,7 +509,8 @@ class Image: 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() diff --git a/PIL/ImagePalette.py b/PIL/ImagePalette.py index c6c05d162..b5f07ee61 100644 --- a/PIL/ImagePalette.py +++ b/PIL/ImagePalette.py @@ -34,6 +34,18 @@ class ImagePalette: (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