7

I have a question about the pixel values returned from an image opened with PIL load function. I am using the following code:

frame = Image.open(fname).load()
a = frame[10, 10]

If I load a GIF image, a is the integer value 43. But if I convert the image a JPEG and rerun the code, a is a tuple (253, 254, 100).

Why? And how can i convert (253, 254, 100) back to 43?

user
  • 5,370
  • 8
  • 47
  • 75
alex
  • 73
  • 1
  • 3

1 Answers1

8

GIFs are pallettized, whereas JPEGs are RGB. The act of transforming the image disposes of the palette, so you will have to look through the pallette entries in the GIF to find the closest match to the desired color.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • To further clarify: a GIF image has a table of (up to) 256 colours. Each pixel has an index into this table (in your example, 10) which is used to determine its colour. JPEGs store the RGB values for each pixel. If you look at entry 10 in the GIF's colour table, it should be ``(253, 254, 100)`` (or very close to it). – Blair Apr 27 '11 at 06:22