Merge pull request #1985 from wiredfool/palette

Use bytearray for mutable palette storage
This commit is contained in:
wiredfool 2016-06-25 15:14:07 +01:00 committed by GitHub
commit c1d25d9062
3 changed files with 9 additions and 2 deletions

View File

@ -38,7 +38,7 @@ class ImagePalette(object):
def __init__(self, mode="RGB", palette=None, size=0):
self.mode = mode
self.rawmode = None # if set, palette contains raw data
self.palette = palette or list(range(256))*len(self.mode)
self.palette = palette or bytearray(range(256))*len(self.mode)
self.colors = {}
self.dirty = None
if ((size == 0 and len(self.mode)*256 != len(self.palette)) or
@ -98,7 +98,7 @@ class ImagePalette(object):
except KeyError:
# allocate new color slot
if isinstance(self.palette, bytes):
self.palette = [int(x) for x in self.palette]
self.palette = bytearray(self.palette)
index = len(self.colors)
if index >= 256:
raise ValueError("cannot allocate more than 256 colors")

BIN
Tests/images/chi.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

View File

@ -52,6 +52,13 @@ class TestImageDraw(PillowTestCase):
self.assertRaises(Exception, lambda: draw.setink(0))
self.assertRaises(Exception, lambda: draw.setfill(0))
def test_valueerror(self):
im = Image.open("Tests/images/chi.gif")
draw = ImageDraw.Draw(im)
draw.line(((0, 0)), fill=(0, 0, 0))
del draw
def test_mode_mismatch(self):
im = hopper("RGB").copy()