diff options
author | AlexSm <alex@ydb.tech> | 2024-01-26 16:00:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-26 16:00:50 +0100 |
commit | 7ebcfd058d924bcc8c23da70e034f7415687885c (patch) | |
tree | e4f00d163c77528c1855f2d7af54a8be83fc1ccb /contrib/python/Pillow/py3/PIL/ImagePalette.py | |
parent | 64ca2dcd06312b9eef624054ceb5f787e11be79a (diff) | |
parent | 6d79e7793c2c462134f4b4a7d911abc7b9b0766f (diff) | |
download | ydb-7ebcfd058d924bcc8c23da70e034f7415687885c.tar.gz |
Merge pull request #1260 from ydb-platform/mergelibs10
mergelibs10
Diffstat (limited to 'contrib/python/Pillow/py3/PIL/ImagePalette.py')
-rw-r--r-- | contrib/python/Pillow/py3/PIL/ImagePalette.py | 68 |
1 files changed, 32 insertions, 36 deletions
diff --git a/contrib/python/Pillow/py3/PIL/ImagePalette.py b/contrib/python/Pillow/py3/PIL/ImagePalette.py index f0c0947086..fbcfa309d2 100644 --- a/contrib/python/Pillow/py3/PIL/ImagePalette.py +++ b/contrib/python/Pillow/py3/PIL/ImagePalette.py @@ -15,6 +15,7 @@ # # See the README file for information on usage and redistribution. # +from __future__ import annotations import array @@ -102,6 +103,30 @@ class ImagePalette: # Declare tostring as an alias for tobytes tostring = tobytes + def _new_color_index(self, image=None, e=None): + if not isinstance(self.palette, bytearray): + self._palette = bytearray(self.palette) + index = len(self.palette) // 3 + special_colors = () + if image: + special_colors = ( + image.info.get("background"), + image.info.get("transparency"), + ) + while index in special_colors: + index += 1 + if index >= 256: + if image: + # Search for an unused index + for i, count in reversed(list(enumerate(image.histogram()))): + if count == 0 and i not in special_colors: + index = i + break + if index >= 256: + msg = "cannot allocate more than 256 colors" + raise ValueError(msg) from e + return index + def getcolor(self, color, image=None): """Given an rgb tuple, allocate palette entry. @@ -124,27 +149,7 @@ class ImagePalette: return self.colors[color] except KeyError as e: # allocate new color slot - if not isinstance(self.palette, bytearray): - self._palette = bytearray(self.palette) - index = len(self.palette) // 3 - special_colors = () - if image: - special_colors = ( - image.info.get("background"), - image.info.get("transparency"), - ) - while index in special_colors: - index += 1 - if index >= 256: - if image: - # Search for an unused index - for i, count in reversed(list(enumerate(image.histogram()))): - if count == 0 and i not in special_colors: - index = i - break - if index >= 256: - msg = "cannot allocate more than 256 colors" - raise ValueError(msg) from e + index = self._new_color_index(image, e) self.colors[color] = index if index * 3 < len(self.palette): self._palette = ( @@ -200,20 +205,15 @@ def raw(rawmode, data): def make_linear_lut(black, white): - lut = [] if black == 0: - for i in range(256): - lut.append(white * i // 255) - else: - raise NotImplementedError # FIXME - return lut + return [white * i // 255 for i in range(256)] + + msg = "unavailable when black is non-zero" + raise NotImplementedError(msg) # FIXME def make_gamma_lut(exp): - lut = [] - for i in range(256): - lut.append(int(((i / 255.0) ** exp) * 255.0 + 0.5)) - return lut + return [int(((i / 255.0) ** exp) * 255.0 + 0.5) for i in range(256)] def negative(mode="RGB"): @@ -225,9 +225,7 @@ def negative(mode="RGB"): def random(mode="RGB"): from random import randint - palette = [] - for i in range(256 * len(mode)): - palette.append(randint(0, 255)) + palette = [randint(0, 255) for _ in range(256 * len(mode))] return ImagePalette(mode, palette) @@ -256,8 +254,6 @@ def load(filename): if lut: break except (SyntaxError, ValueError): - # import traceback - # traceback.print_exc() pass else: msg = "cannot load palette" |