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
|
2014-07-20 11:13:26 +04:00
|
|
|
import warnings
|
2014-07-22 00:18:46 +04:00
|
|
|
from PIL import ImageColor
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class ImagePalette(object):
|
2013-10-14 08:22:18 +04:00
|
|
|
"Color palette for palette mapped images"
|
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
|
2012-10-16 01:18:27 +04:00
|
|
|
self.palette = palette or list(range(256))*len(self.mode)
|
2010-07-31 06:52:47 +04:00
|
|
|
self.colors = {}
|
|
|
|
self.dirty = None
|
2014-07-15 19:38:10 +04:00
|
|
|
if ((size == 0 and len(self.mode)*256 != len(self.palette)) or
|
2013-12-04 18:07:36 +04:00
|
|
|
(size != 0 and size != len(self.palette))):
|
2012-10-11 07:52:53 +04:00
|
|
|
raise ValueError("wrong palette size")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def getdata(self):
|
2013-10-14 08:22:18 +04: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
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return self.mode + ";L", self.tobytes()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
def tobytes(self):
|
2013-10-14 08:22:18 +04:00
|
|
|
"""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")
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if isinstance(self.palette, bytes):
|
2010-07-31 06:52:47 +04:00
|
|
|
return self.palette
|
2013-05-23 21:33:27 +04:00
|
|
|
arr = array.array("B", self.palette)
|
|
|
|
if hasattr(arr, 'tobytes'):
|
2014-07-15 19:38:10 +04:00
|
|
|
# py3k has a tobytes, tostring is deprecated.
|
2013-05-23 21:33:27 +04:00
|
|
|
return arr.tobytes()
|
|
|
|
return arr.tostring()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-03-07 13:23:40 +04:00
|
|
|
# Declare tostring as an alias for tobytes
|
|
|
|
tostring = tobytes
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def getcolor(self, color):
|
2013-10-14 08:22:18 +04:00
|
|
|
"""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")
|
2012-10-16 07:14:10 +04:00
|
|
|
if isinstance(color, tuple):
|
2010-07-31 06:52:47 +04:00
|
|
|
try:
|
|
|
|
return self.colors[color]
|
|
|
|
except KeyError:
|
|
|
|
# allocate new color slot
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if isinstance(self.palette, bytes):
|
2012-10-16 05:58:46 +04:00
|
|
|
self.palette = [int(x) for x in self.palette]
|
2010-07-31 06:52:47 +04: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):
|
2013-10-14 08:22:18 +04:00
|
|
|
"""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")
|
2012-10-17 07:39:56 +04:00
|
|
|
if isinstance(fp, str):
|
2010-07-31 06:52:47 +04: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-06-24 12:27:35 +04:00
|
|
|
for j in range(i*len(self.mode), (i+1)*len(self.mode)):
|
2014-06-24 11:34:43 +04:00
|
|
|
try:
|
|
|
|
fp.write(" %d" % 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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def _make_linear_lut(black, white):
|
2014-07-20 11:13:26 +04:00
|
|
|
warnings.warn(
|
|
|
|
'_make_linear_lut() is deprecated. '
|
|
|
|
'Please call make_linear_lut() instead.',
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2
|
|
|
|
)
|
|
|
|
return make_linear_lut(black, white)
|
|
|
|
|
|
|
|
|
|
|
|
def _make_gamma_lut(exp):
|
|
|
|
warnings.warn(
|
|
|
|
'_make_gamma_lut() is deprecated. '
|
|
|
|
'Please call make_gamma_lut() instead.',
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2
|
|
|
|
)
|
|
|
|
return make_gamma_lut(exp)
|
|
|
|
|
|
|
|
|
|
|
|
def make_linear_lut(black, white):
|
2010-07-31 06:52:47 +04:00
|
|
|
lut = []
|
|
|
|
if black == 0:
|
|
|
|
for i in range(256):
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04: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"):
|
2012-10-16 01:18:27 +04:00
|
|
|
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
|
|
|
|
palette = []
|
|
|
|
for i in range(256*len(mode)):
|
|
|
|
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)
|
2014-07-20 22:08:14 +04:00
|
|
|
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"):
|
2012-10-16 01:18:27 +04:00
|
|
|
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
|
|
|
|
|
|
|
|
fp = open(filename, "rb")
|
|
|
|
|
|
|
|
lut = None
|
|
|
|
|
|
|
|
if not lut:
|
|
|
|
try:
|
2013-03-07 20:20:28 +04:00
|
|
|
from PIL import GimpPaletteFile
|
2010-07-31 06:52:47 +04:00
|
|
|
fp.seek(0)
|
|
|
|
p = GimpPaletteFile.GimpPaletteFile(fp)
|
|
|
|
lut = p.getpalette()
|
|
|
|
except (SyntaxError, ValueError):
|
2014-07-15 19:38:10 +04:00
|
|
|
# import traceback
|
|
|
|
# traceback.print_exc()
|
2010-07-31 06:52:47 +04:00
|
|
|
pass
|
|
|
|
|
|
|
|
if not lut:
|
|
|
|
try:
|
2013-03-07 20:20:28 +04:00
|
|
|
from PIL import GimpGradientFile
|
2010-07-31 06:52:47 +04:00
|
|
|
fp.seek(0)
|
|
|
|
p = GimpGradientFile.GimpGradientFile(fp)
|
|
|
|
lut = p.getpalette()
|
|
|
|
except (SyntaxError, ValueError):
|
2014-07-15 19:38:10 +04:00
|
|
|
# import traceback
|
|
|
|
# traceback.print_exc()
|
2010-07-31 06:52:47 +04:00
|
|
|
pass
|
|
|
|
|
|
|
|
if not lut:
|
|
|
|
try:
|
2013-03-07 20:20:28 +04:00
|
|
|
from PIL import PaletteFile
|
2010-07-31 06:52:47 +04:00
|
|
|
fp.seek(0)
|
|
|
|
p = PaletteFile.PaletteFile(fp)
|
|
|
|
lut = p.getpalette()
|
|
|
|
except (SyntaxError, ValueError):
|
2015-04-03 03:49:04 +03:00
|
|
|
#import traceback
|
|
|
|
#traceback.print_exc()
|
2010-07-31 06:52:47 +04:00
|
|
|
pass
|
|
|
|
|
|
|
|
if not lut:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise IOError("cannot load palette")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-07-15 19:38:10 +04:00
|
|
|
return lut # data, rawmode
|