简体   繁体   中英

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 rgb 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...

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
>>> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM