[Image-SIG] Converting to a specific palette

James Kew james.kew@btinternet.com
Wed, 11 Sep 2002 23:39:21 +0100


Any how-to clues? I'm interested in processing arbitrary images -- usually
RGB mode  -- for display on hardware which is basically palette-based, with
4- or 8-bit pixels and corresponding 16- or 256-entry palettes.

Converting to the 240-entry "web" palette is easy:

im = PIL.Image.load(<filename>)
im_web = im.convert("P")

and, once I read the PIL source, converting to an optimal N-entry palette is
easy:

im_256 = im.convert("P", palette=PIL.Image.ADAPTIVE, colors=256)
im_16 = im.convert("P", palette=PIL.Image.ADAPTIVE, colors=16)

However, my reading of the docs and source so far hasn't enlightened me on
how to take an arbitrary image and map it onto an existing palette. Is this
something that PIL supports, or do I need to roll (or find) my own?

(The following threads address the same or similar questions:
http://mail.python.org/pipermail/image-sig/2001-March/001375.html
http://mail.python.org/pipermail/image-sig/2002-March/001789.html
and suggest this is addressed in 1.1.3. But: I'm using the PIL 1.1.2 binary
on Python 2.2.1 on Windows; I'm not well-equipped to build the 1.1.3 source
distribution; pythonware doesn't have 1.1.3 binaries yet (how much later
than 6 months is "later"?); and the "experimental" effbot installer is for
Python 2.1.)

--
James Kew
james.kew@btinternet.com