[Tutor] PIL's palette

Peter Szinek peter at rt.sk
Fri Jul 15 08:18:55 CEST 2005


D. Hartley wrote:
>>Your palette is a LookUp Table (LUT) - it is also named like this in
>>your prog ('lut') - which is a 768-long list (or as in this case, a list
>>of 256 lists, every inner list has 3 elements - so again you have 768
>>elements). So 3x256 or 1x768 is just an implementation detail. Let's see
>>the 3x256 version:
> Actually, it creates one list, of 768 values (numbers).  Not a list of
> lists, which could be indexed into.  Just one lonnnnng list.  So I
> dont know how it indexes into it or does anything with it! But I
> printed out "lut" and that's what it is, one long list. Anybody know
> how that works, then?

As i ave said, this is just an implementation detail. Whether you have this:

lut = [ [10,10,10], [20,20,20], [30,30,30] ]

or this

lut = [ 10,10,10,20,20,20,30,30,30 ]

it represents the same - int this case 3 colors. Just in the first case 
you would do it like this (assuming that color is a list of length 3):

color1 = lut[0]
color2 = lut[1]
color3 = lut[2]

and in the other case (which is according to you the actual implementation):

color1 = [ lut[0], lut[1], lut[2] ]
color2 = [ lut[3], lut[4], lut[5] ]
color3 = [ lut[6], lut[7], lut[8] ]


color_i = [ lut[(i-1)*3], lut[(i-1)*3+1], lut[(i-1)*3+2] ]

>>Now your picture looks like this:
> Even if it *did* have indices - why would my picture look like this,

Well, (correct me if i am wrong, but IMHO) the very idea of an image 
with a palette is that it has indices rather than colors directly, i.e. 
in non-paletted image (like a bmp) when you ask 'what pixel is at 
coordinate (x,y)?' then you get back an (r,g,b) tuple (or list, 
whatever) whereas when working with a paletted one (like gif), you get 
back an index. The index you get back is an index to the palette, so if 
you get back e.g. 42, it means that the color of the pixel at coordinate 
x,y is the 42nd color of the palette. At least this is what i remember,
it may be other way with PIL, but i have slight doubts.

> now? I dont think I understand what you're saying. 

> Is this after I do the putpalette? All I know is, I start with one image, I do
> putpalette() and all of a sudden everything is a different color. And
> I dont know why it changes which colors to which. The greyscale thing
> makes a little more sense - it turns my color image into a black and
> white one, but I dont know why it chooses which grey color for which
> original color.

Look, the point is this: If the pixel at the coordinate (0,0) has the 
index 25, and the palette has blue at position 25, then in the rendering 
cycle a blue pixel will be rendered at (0,0). But if you change the 
palette, altering the 25th color to green, and render again, now the 
pixel (0,0) will be rendered as green. putpalette() does exactly this - 
it changes the palette! So the indices suddenly represent an other color 
then before! That's why the change of colors.
>>And when it is rendered, PIL looks up the color (hence lookup table) at
>>the given index. So the first pixel of the image will be the color held
>>at index 8 in the lookup table (or palette).
> Why would the first pixel be at index 8? 
It was just an example. You may have ther any number between 0 and 255.
> What color is it looking up?
> I'm starting with an image (say it's a red square with a blue square
> inside it, that's more simple). What is it looking up? Pixel 1 already
> *has* a color - (255,0,0). 
Yes, but it has a color (255,0,0) because the index at pixel 1 points to 
a place in the palette which has color (255,0,0)
  Putpalette() obviously ... picks some
> other value. I don't know why, or how. 
putpalette() changes the palette, so now the index at pixel 1 will point 
to the changed color

All the pixels in the original
> image start with a color value, and after putpalette() they end up
> with a different color value. I just dont know how it decides that, or
> why (and thus I dont know how I can control it to use it the way I
> want - for instance if I wanted to make the square yellow on the
> inside instead of blue).
>>So basically the palette is the comupter abstraction of a real painter's
>>palette - the real painter has x colors on his palette, and says (by
>>looking at the palette): ah, i need this intelligent shade of cobalt
>>blue. You do the same here - you have a palette (the look up table) and
>>say: ah, i need this color - so you put the INDEX of that color into the
>>image, and when the computer is 'painting' he looks up the color at that
> Again - I dont know how I'm telling the palette what color I want. I
> don't feel like I am telling it anything, at this point, ah ha. I just
> have a list of 768 numbers, which python somehow uses to change all
> the colors in my previous image.

The 768 numbers is THE palette.
Des this make it better now? If not, feel free to ask further, i hope we 
can fledge this out ;-)


More information about the Tutor mailing list