PIL and miscolored images

Fredrik Lundh fredrik at pythonware.com
Sun Jun 16 22:06:16 CEST 2002

Markus von Ehr wrote:
> I'd like to show a greylevel image as miscolored image,
> anyone has an idea how i can do it and maybe adjust
> the color scale?

do you mean pseudocoloring?  something like this should

    from PIL import Image

    im = Image.open("some grayscale image")
    im.load() # make sure it's loaded into memory

    assert im.mode == "L"

    # create a lookup table (r, g, b, r, g, b, r, g, b, ...)
    lut = []
    for i in range(256):
        lut.extend([255-i, i/2, i])

    assert im.mode == "P" # now has a palette


the "load" call is a workaround for a bug in 1.1.3 and earlier.

without it, you will sometimes get a ValueError exception when
attempting to save (or further process) the image.


More information about the Python-list mailing list