2013-10-11 17:28:32 +00: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
|
2014-09-30 16:15:32 +00:00
|
|
|
from PIL import Image, ImageColor
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ImagePalette:
|
2014-09-30 16:15:32 +00:00
|
|
|
"Color palette for palette mapped images"
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
def __init__(self, mode = "RGB", palette = None, size = 0):
|
2013-10-11 17:28:32 +00:00
|
|
|
self.mode = mode
|
|
|
|
self.rawmode = None # if set, palette contains raw data
|
2014-09-30 16:15:32 +00:00
|
|
|
self.palette = palette or list(range(256))*len(self.mode)
|
2013-10-11 17:28:32 +00:00
|
|
|
self.colors = {}
|
|
|
|
self.dirty = None
|
2014-09-30 16:15:32 +00:00
|
|
|
if ((size == 0 and len(self.mode)*256 != len(self.palette)) or
|
|
|
|
(size != 0 and size != len(self.palette))):
|
|
|
|
raise ValueError("wrong palette size")
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def getdata(self):
|
2014-09-30 16:15:32 +00:00
|
|
|
"""
|
|
|
|
Get palette contents in format suitable # for the low-level
|
|
|
|
``im.putpalette`` primitive.
|
|
|
|
|
|
|
|
.. warning:: This method is experimental.
|
|
|
|
"""
|
2013-10-11 17:28:32 +00:00
|
|
|
if self.rawmode:
|
|
|
|
return self.rawmode, self.palette
|
2014-09-30 16:15:32 +00:00
|
|
|
return self.mode + ";L", self.tobytes()
|
|
|
|
|
|
|
|
def tobytes(self):
|
|
|
|
"""Convert palette to bytes.
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
.. warning:: This method is experimental.
|
|
|
|
"""
|
2013-10-11 17:28:32 +00:00
|
|
|
if self.rawmode:
|
|
|
|
raise ValueError("palette contains raw palette data")
|
2014-09-30 16:15:32 +00:00
|
|
|
if isinstance(self.palette, bytes):
|
2013-10-11 17:28:32 +00:00
|
|
|
return self.palette
|
2014-09-30 16:15:32 +00:00
|
|
|
arr = array.array("B", self.palette)
|
|
|
|
if hasattr(arr, 'tobytes'):
|
|
|
|
#py3k has a tobytes, tostring is deprecated.
|
|
|
|
return arr.tobytes()
|
|
|
|
return arr.tostring()
|
|
|
|
|
|
|
|
# Declare tostring as an alias for tobytes
|
|
|
|
tostring = tobytes
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def getcolor(self, color):
|
2014-09-30 16:15:32 +00:00
|
|
|
"""Given an rgb tuple, allocate palette entry.
|
|
|
|
|
|
|
|
.. warning:: This method is experimental.
|
|
|
|
"""
|
2013-10-11 17:28:32 +00:00
|
|
|
if self.rawmode:
|
|
|
|
raise ValueError("palette contains raw palette data")
|
2014-09-30 16:15:32 +00:00
|
|
|
if isinstance(color, tuple):
|
2013-10-11 17:28:32 +00:00
|
|
|
try:
|
|
|
|
return self.colors[color]
|
|
|
|
except KeyError:
|
|
|
|
# allocate new color slot
|
2014-09-30 16:15:32 +00:00
|
|
|
if isinstance(self.palette, bytes):
|
|
|
|
self.palette = [int(x) for x in self.palette]
|
2013-10-11 17:28:32 +00:00
|
|
|
index = len(self.colors)
|
|
|
|
if index >= 256:
|
|
|
|
raise ValueError("cannot allocate more than 256 colors")
|
|
|
|
self.colors[color] = index
|
|
|
|
self.palette[index] = color[0]
|
|
|
|
self.palette[index+256] = color[1]
|
|
|
|
self.palette[index+512] = color[2]
|
|
|
|
self.dirty = 1
|
|
|
|
return index
|
|
|
|
else:
|
|
|
|
raise ValueError("unknown color specifier: %r" % color)
|
|
|
|
|
|
|
|
def save(self, fp):
|
2014-09-30 16:15:32 +00:00
|
|
|
"""Save palette to text file.
|
|
|
|
|
|
|
|
.. warning:: This method is experimental.
|
|
|
|
"""
|
2013-10-11 17:28:32 +00:00
|
|
|
if self.rawmode:
|
|
|
|
raise ValueError("palette contains raw palette data")
|
2014-09-30 16:15:32 +00:00
|
|
|
if isinstance(fp, str):
|
2013-10-11 17:28:32 +00:00
|
|
|
fp = open(fp, "w")
|
|
|
|
fp.write("# Palette\n")
|
|
|
|
fp.write("# Mode: %s\n" % self.mode)
|
|
|
|
for i in range(256):
|
|
|
|
fp.write("%d" % i)
|
2014-09-30 16:15:32 +00:00
|
|
|
for j in range(i*len(self.mode), (i+1)*len(self.mode)):
|
|
|
|
try:
|
|
|
|
fp.write(" %d" % self.palette[j])
|
|
|
|
except IndexError:
|
|
|
|
fp.write(" 0")
|
2013-10-11 17:28:32 +00:00
|
|
|
fp.write("\n")
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Internal
|
|
|
|
|
|
|
|
def raw(rawmode, data):
|
|
|
|
palette = ImagePalette()
|
|
|
|
palette.rawmode = rawmode
|
|
|
|
palette.palette = data
|
|
|
|
palette.dirty = 1
|
|
|
|
return palette
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Factories
|
|
|
|
|
|
|
|
def _make_linear_lut(black, white):
|
|
|
|
lut = []
|
|
|
|
if black == 0:
|
|
|
|
for i in range(256):
|
2014-09-30 16:15:32 +00:00
|
|
|
lut.append(white*i//255)
|
2013-10-11 17:28:32 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError # FIXME
|
|
|
|
return lut
|
|
|
|
|
|
|
|
def _make_gamma_lut(exp, mode="RGB"):
|
|
|
|
lut = []
|
|
|
|
for i in range(256):
|
|
|
|
lut.append(int(((i / 255.0) ** exp) * 255.0 + 0.5))
|
|
|
|
return lut
|
|
|
|
|
|
|
|
def new(mode, data):
|
|
|
|
return Image.core.new_palette(mode, data)
|
|
|
|
|
|
|
|
def negative(mode="RGB"):
|
2014-09-30 16:15:32 +00:00
|
|
|
palette = list(range(256))
|
2013-10-11 17:28:32 +00:00
|
|
|
palette.reverse()
|
|
|
|
return ImagePalette(mode, palette * len(mode))
|
|
|
|
|
|
|
|
def random(mode="RGB"):
|
|
|
|
from random import randint
|
|
|
|
palette = []
|
|
|
|
for i in range(256*len(mode)):
|
|
|
|
palette.append(randint(0, 255))
|
|
|
|
return ImagePalette(mode, palette)
|
|
|
|
|
|
|
|
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)
|
|
|
|
return ImagePalette("RGB", r + g + b)
|
|
|
|
|
|
|
|
def wedge(mode="RGB"):
|
2014-09-30 16:15:32 +00:00
|
|
|
return ImagePalette(mode, list(range(256)) * len(mode))
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def load(filename):
|
|
|
|
|
|
|
|
# FIXME: supports GIMP gradients only
|
|
|
|
|
|
|
|
fp = open(filename, "rb")
|
|
|
|
|
|
|
|
lut = None
|
|
|
|
|
|
|
|
if not lut:
|
|
|
|
try:
|
2014-09-30 16:15:32 +00:00
|
|
|
from PIL import GimpPaletteFile
|
2013-10-11 17:28:32 +00:00
|
|
|
fp.seek(0)
|
|
|
|
p = GimpPaletteFile.GimpPaletteFile(fp)
|
|
|
|
lut = p.getpalette()
|
|
|
|
except (SyntaxError, ValueError):
|
2014-09-30 16:15:32 +00:00
|
|
|
#import traceback
|
|
|
|
#traceback.print_exc()
|
2013-10-11 17:28:32 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
if not lut:
|
|
|
|
try:
|
2014-09-30 16:15:32 +00:00
|
|
|
from PIL import GimpGradientFile
|
2013-10-11 17:28:32 +00:00
|
|
|
fp.seek(0)
|
|
|
|
p = GimpGradientFile.GimpGradientFile(fp)
|
|
|
|
lut = p.getpalette()
|
|
|
|
except (SyntaxError, ValueError):
|
2014-09-30 16:15:32 +00:00
|
|
|
#import traceback
|
|
|
|
#traceback.print_exc()
|
2013-10-11 17:28:32 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
if not lut:
|
|
|
|
try:
|
2014-09-30 16:15:32 +00:00
|
|
|
from PIL import PaletteFile
|
2013-10-11 17:28:32 +00:00
|
|
|
fp.seek(0)
|
|
|
|
p = PaletteFile.PaletteFile(fp)
|
|
|
|
lut = p.getpalette()
|
|
|
|
except (SyntaxError, ValueError):
|
2014-09-30 16:15:32 +00:00
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2013-10-11 17:28:32 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
if not lut:
|
2014-09-30 16:15:32 +00:00
|
|
|
raise IOError("cannot load palette")
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
return lut # data, rawmode
|