[Image-SIG] Re: Returning Colormap values for an image?

Alfred Milgrom milgrom at gmail.com
Fri Aug 13 08:50:51 CEST 2004


Hi:

Thanks for your reply. It took me a while to understand what was going
on, but I have finally figured it out!

As I understand it, the resize does not change the palette
information, and when you fill the data with range(256) and convert to
RGB each value is mapped to the corresponding palette entry.

The only problem (if you can call it that?) is that the colours
actually used in the original image are not necessarily the first n
values in the lut image, and so it is necessary to not only work out
how many colours are used initially but also which indices are used.

Is there an easier way to get a list of the indices used than something like:

hist = im.histogram()
coloursUsed = [i for i in range(len(hist)) if hist[i]]

Also, if the image I have is an RGB image (such as the result of an
ImageGrab), what is the best way to get the colours used? Is
converting to 'P' mode and then using the resize/convert method the
easiest/fastest?

Thanks again,
Alfred Milgrom

On Thu, 12 Aug 2004 10:42:21 +0200, Fredrik Lundh
<fredrik at pythonware.com> wrote:
> Alfred Milgrom wrote:
> 
> > Unfortunately I think I am missing something as far as the
> > resize/convert technique is concerned. Basically I do not understand
> > why all the colours will be preserved if the image is resized, nor why
> > the colours will then be neatly arranged in the lut Image.
> 
> that's an interesting question: the example is broken, and you're the
> first one who's noticed.  oops!  here's how it's supposed to look:
> 
>     lut = im.resize((256, 1))
>     lut.putdata(range(256))
>     lut = lut.convert("RGB").getdata()
> 
> (the resulting lut object is now a 256-item sequence of (r, g, b) tuples;
> use list(lut) if you want to print it)


More information about the Image-SIG mailing list