Reputation: 1952
I am working on a very basic PNG image, but when I try to load it in, it gives all pixels as (0). Image link: https://i.sstatic.net/GTTVg.png
from PIL import Image
myImage = Image.open("Oook1VX.png")
print(myImage.getpixel((0,0)))
print(myImage.getcolors())
Output:
0
[(2073600, 0)]
I want it to be able to see the green? It worked for other images, but not this one. If anyone has any ideas I would be very appreciative.
Upvotes: 0
Views: 598
Reputation: 15905
getcolors()
returns a list of tuples that map colors to numbers (for compression purposes).
In your example, that list of tuples says that the color 2073600
is encoded as 0
in the image. So if getpixel()
returns 0
that means 2073600
.
2073600
is #1fa400 in hex, which is the green in your image.
You might benefit from a wrapper like this one that automatically resolves the colors:
import struct
class PngImage:
def __init__(self, image):
self.image = image
self._colors = {index: color for color, index in image.getcolors()}
def getpixel(self, pos):
color = self._colors[self.image.getpixel(pos)]
return struct.unpack('3B', struct.pack('I', color))
image = PngImage(Image.open("Oook1VX.png"))
image.getpixel((0, 0)) # => (0x1f, 0x1f, 0x00)
Upvotes: 1