PIL and miscolored images

Fredrik Lundh fredrik at pythonware.com
Sun Jun 16 16:06:16 EDT 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
work:

    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])
    im.putpalette(lut)

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

    im.save("out.gif")

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.

</F>





More information about the Python-list mailing list