From aa4489c9936d3215c0e3adb73638aac117d3681e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 19 Jun 2021 09:31:46 +1000 Subject: [PATCH] Keep palette the same length --- src/PIL/Image.py | 4 ++-- src/_imaging.c | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 1dfc55b0d..8fb7ea576 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -830,7 +830,7 @@ class Image: arr = bytes( value for (index, value) in enumerate(arr) if index % 4 != 3 ) - self.im.putpalette(mode, arr) + palette_length = self.im.putpalette(mode, arr) self.palette.dirty = 0 self.palette.rawmode = None if "transparency" in self.info: @@ -841,7 +841,7 @@ class Image: self.palette.mode = "RGBA" else: self.palette.mode = "RGB" - self.palette.palette = self.im.getpalette() + self.palette.palette = self.im.getpalette()[: palette_length * 3] if self.im: if cffi and USE_CFFI_ACCESS: diff --git a/src/_imaging.c b/src/_imaging.c index 28acbf35a..e2193fec3 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1663,8 +1663,7 @@ _putpalette(ImagingObject *self, PyObject *args) { unpack(self->image->palette->palette, palette, palettesize * 8 / bits); - Py_INCREF(Py_None); - return Py_None; + return PyLong_FromLong(palettesize * 8 / bits); } static PyObject *