Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RGB Values issue using PIL in python 3 issues

I am using the PIL Library in Python 3 to modify .gif files and in most of the cases so far the end result has looked how it is supposed to, but when checking some rgb values I noticed that I am only getting a few numbers back.

0, 51, 153, 102 are 4 of the values I get most often. There may be 1-2 others, but that would be it. In a 200x200 gif image I am only getting 4-6 r g b values.

Here is a copy the generic format I am currently using:

from PIL import Image

def main():
    image=Image.open("filename.gif")
    image=image.convert('RGB')
    width, height = image.size
    for x in range(width):
        for y in range(height):

            r, g, b = image.getpixel ((x,y))
            print (r,g,b)

I was trying to right shift by 4 to transfer the high bits to low bits, but the return is almost always that of 0.

Am I doing something wrong in the coding? I think its causing some problems when trying to left right shift the bit values.

Any help would be appreciated...

like image 495
Eric May Avatar asked Sep 14 '26 23:09

Eric May


1 Answers

There's nothing wrong with the code you've posted.

The complete list of values you're getting back is no doubt 0, 51, 102, 153, 204 and 255, or in hexadecimal: 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff (which represent intensities from 0% to 100%, in steps of 20%). Since there are six of these, the total number of colors they can represent is 6 * 6 * 6 = 216, and since 216 < 256, that means they fit inside the GIF format's 256-color palette.

These are also known as web-safe colors, and for historical reasons they're often used in GIF images.

Your right-shift problem would seem to be unrelated, however:

>>> for x in 0, 51, 102, 153, 204, 255:
...     print x >> 4
... 
0
3
6
9
12
15
>>> 
like image 94
Zero Piraeus Avatar answered Sep 18 '26 03:09

Zero Piraeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!