summaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py3/PIL/ImagePalette.py
diff options
context:
space:
mode:
authorkickbutt <[email protected]>2024-01-23 23:36:43 +0300
committerkickbutt <[email protected]>2024-01-23 23:55:22 +0300
commitfe742a0b69a530f86d1ea7aa84978d673256f8b7 (patch)
treea045a5eb8dba770797e84d0b233098605396027d /contrib/python/Pillow/py3/PIL/ImagePalette.py
parentbd7d89b121ae7b9f4427766292c950fcc91c2975 (diff)
Fix separator in CUDA_ARCHITECTURES
Diffstat (limited to 'contrib/python/Pillow/py3/PIL/ImagePalette.py')
-rw-r--r--contrib/python/Pillow/py3/PIL/ImagePalette.py68
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 f0c09470863..fbcfa309d29 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"