Pillow/src/PIL/ImagePalette.py

250 lines
7.1 KiB
Python
Raw Normal View History

2010-07-31 06:52:47 +04:00
#
# The Python Imaging Library.
# $Id$
#
# image palette object
#
# History:
# 1996-03-11 fl Rewritten.
# 1997-01-03 fl Up and running.
# 1997-08-23 fl Added load hack
# 2001-04-16 fl Fixed randint shadow bug in random()
#
# Copyright (c) 1997-2001 by Secret Labs AB
# Copyright (c) 1996-1997 by Fredrik Lundh
#
# See the README file for information on usage and redistribution.
#
import array
from . import GimpGradientFile, GimpPaletteFile, ImageColor, PaletteFile
2010-07-31 06:52:47 +04:00
class ImagePalette:
2015-08-20 22:25:08 +03:00
"""
Color palette for palette mapped images
:param mode: The mode to use for the Palette. See:
:ref:`concept-modes`. Defaults to "RGB"
:param palette: An optional palette. If given, it must be a bytearray,
2015-09-11 12:28:19 +03:00
an array or a list of ints between 0-255 and of length ``size``
2015-08-20 22:25:08 +03:00
times the number of colors in ``mode``. The list must be aligned
by channel (All R values must be contiguous in the list before G
and B values.) Defaults to 0 through 255 per channel.
:param size: An optional palette size. If given, it cannot be equal to
or greater than 256. Defaults to 0.
"""
2010-07-31 06:52:47 +04:00
2014-07-15 19:38:10 +04:00
def __init__(self, mode="RGB", palette=None, size=0):
2010-07-31 06:52:47 +04:00
self.mode = mode
2014-07-15 19:38:10 +04:00
self.rawmode = None # if set, palette contains raw data
2021-06-23 12:22:21 +03:00
self.palette = palette or bytearray()
2010-07-31 06:52:47 +04:00
self.dirty = None
2021-06-23 12:22:21 +03:00
if size != 0 and size != len(self.palette):
raise ValueError("wrong palette size")
2010-07-31 06:52:47 +04:00
2021-06-23 12:16:04 +03:00
@property
def palette(self):
return self._palette
@palette.setter
def palette(self, palette):
self._palette = palette
mode_len = len(self.mode)
2021-06-27 09:33:47 +03:00
self.colors = {}
for i in range(0, len(self.palette), mode_len):
color = tuple(self.palette[i : i + mode_len])
if color in self.colors:
continue
self.colors[color] = i // mode_len
2021-06-23 12:16:04 +03:00
def copy(self):
new = ImagePalette()
new.mode = self.mode
new.rawmode = self.rawmode
if self.palette is not None:
new.palette = self.palette[:]
new.dirty = self.dirty
return new
2010-07-31 06:52:47 +04:00
def getdata(self):
"""
2018-08-11 09:39:49 +03:00
Get palette contents in format suitable for the low-level
``im.putpalette`` primitive.
.. warning:: This method is experimental.
"""
2010-07-31 06:52:47 +04:00
if self.rawmode:
return self.rawmode, self.palette
return self.mode + ";L", self.tobytes()
2010-07-31 06:52:47 +04:00
def tobytes(self):
"""Convert palette to bytes.
.. warning:: This method is experimental.
"""
2010-07-31 06:52:47 +04:00
if self.rawmode:
raise ValueError("palette contains raw palette data")
if isinstance(self.palette, bytes):
2010-07-31 06:52:47 +04:00
return self.palette
arr = array.array("B", self.palette)
2021-06-22 14:57:25 +03:00
return arr.tobytes()
2010-07-31 06:52:47 +04:00
# Declare tostring as an alias for tobytes
tostring = tobytes
def getcolor(self, color, image=None):
"""Given an rgb tuple, allocate palette entry.
.. warning:: This method is experimental.
"""
2010-07-31 06:52:47 +04:00
if self.rawmode:
raise ValueError("palette contains raw palette data")
if isinstance(color, tuple):
if self.mode == "RGB":
if len(color) == 4 and color[3] == 255:
color = color[:3]
elif self.mode == "RGBA":
if len(color) == 3:
color += (255,)
2010-07-31 06:52:47 +04:00
try:
return self.colors[color]
except KeyError as e:
2010-07-31 06:52:47 +04:00
# allocate new color slot
if not isinstance(self.palette, bytearray):
2021-06-23 12:16:04 +03:00
self._palette = bytearray(self.palette)
2021-06-23 12:22:21 +03:00
index = len(self.palette) // 3
2010-07-31 06:52:47 +04:00
if index >= 256:
if image:
# Search for an unused index
for i, count in reversed(list(enumerate(image.histogram()))):
if count == 0:
index = i
break
if index >= 256:
raise ValueError("cannot allocate more than 256 colors") from e
2010-07-31 06:52:47 +04:00
self.colors[color] = index
2021-06-23 12:22:21 +03:00
if index * 3 < len(self.palette):
self._palette = (
self.palette[: index * 3]
+ bytes(color)
+ self.palette[index * 3 + 3 :]
)
2021-06-23 12:22:21 +03:00
else:
self._palette += bytes(color)
2010-07-31 06:52:47 +04:00
self.dirty = 1
return index
else:
raise ValueError(f"unknown color specifier: {repr(color)}")
2010-07-31 06:52:47 +04:00
def save(self, fp):
"""Save palette to text file.
.. warning:: This method is experimental.
"""
2010-07-31 06:52:47 +04:00
if self.rawmode:
raise ValueError("palette contains raw palette data")
if isinstance(fp, str):
2010-07-31 06:52:47 +04:00
fp = open(fp, "w")
fp.write("# Palette\n")
fp.write(f"# Mode: {self.mode}\n")
2010-07-31 06:52:47 +04:00
for i in range(256):
fp.write(f"{i}")
2019-03-21 16:28:20 +03:00
for j in range(i * len(self.mode), (i + 1) * len(self.mode)):
try:
fp.write(f" {self.palette[j]}")
except IndexError:
fp.write(" 0")
2010-07-31 06:52:47 +04:00
fp.write("\n")
fp.close()
2014-07-15 19:38:10 +04:00
2010-07-31 06:52:47 +04:00
# --------------------------------------------------------------------
# Internal
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
def raw(rawmode, data):
palette = ImagePalette()
palette.rawmode = rawmode
palette.palette = data
palette.dirty = 1
return palette
2014-07-15 19:38:10 +04:00
2010-07-31 06:52:47 +04:00
# --------------------------------------------------------------------
# Factories
2019-03-21 16:28:20 +03:00
def make_linear_lut(black, white):
2010-07-31 06:52:47 +04:00
lut = []
if black == 0:
for i in range(256):
2019-03-21 16:28:20 +03:00
lut.append(white * i // 255)
2010-07-31 06:52:47 +04:00
else:
2014-07-15 19:38:10 +04:00
raise NotImplementedError # FIXME
2010-07-31 06:52:47 +04:00
return lut
2014-07-15 19:38:10 +04:00
2014-07-19 02:45:57 +04:00
def make_gamma_lut(exp):
2010-07-31 06:52:47 +04:00
lut = []
for i in range(256):
lut.append(int(((i / 255.0) ** exp) * 255.0 + 0.5))
return lut
2014-07-15 19:38:10 +04:00
2010-07-31 06:52:47 +04:00
def negative(mode="RGB"):
palette = list(range(256))
2010-07-31 06:52:47 +04:00
palette.reverse()
return ImagePalette(mode, palette * len(mode))
2014-07-15 19:38:10 +04:00
2010-07-31 06:52:47 +04:00
def random(mode="RGB"):
from random import randint
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
palette = []
2019-03-21 16:28:20 +03:00
for i in range(256 * len(mode)):
2010-07-31 06:52:47 +04:00
palette.append(randint(0, 255))
return ImagePalette(mode, palette)
2014-07-15 19:38:10 +04:00
2010-07-31 06:52:47 +04:00
def sepia(white="#fff0c0"):
r, g, b = ImageColor.getrgb(white)
r = make_linear_lut(0, r)
g = make_linear_lut(0, g)
b = make_linear_lut(0, b)
2010-07-31 06:52:47 +04:00
return ImagePalette("RGB", r + g + b)
2014-07-15 19:38:10 +04:00
2010-07-31 06:52:47 +04:00
def wedge(mode="RGB"):
return ImagePalette(mode, list(range(256)) * len(mode))
2010-07-31 06:52:47 +04:00
2014-07-15 19:38:10 +04:00
2010-07-31 06:52:47 +04:00
def load(filename):
# FIXME: supports GIMP gradients only
with open(filename, "rb") as fp:
for paletteHandler in [
GimpPaletteFile.GimpPaletteFile,
GimpGradientFile.GimpGradientFile,
2019-03-21 16:28:20 +03:00
PaletteFile.PaletteFile,
]:
try:
fp.seek(0)
lut = paletteHandler(fp).getpalette()
if lut:
break
except (SyntaxError, ValueError):
# import traceback
# traceback.print_exc()
pass
else:
raise OSError("cannot load palette")
2010-07-31 06:52:47 +04:00
2014-07-15 19:38:10 +04:00
return lut # data, rawmode