Dovski14
Dovski14

Reputation: 11

How do I get a (R, G, B) output for a pixel with python?

I'm trying to get the code to give me an output of the rgb value of a certain pixel

Input:

from PIL import Image 

img=Image.open(r"C:\img1.png")

pixelMap = img.load()
pixel = pixelMap[0,0]
print(pixel)

Output:0

Desired Output: (0, 0, 0)

Upvotes: 1

Views: 46

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207445

Your image is probably a palette image - see here. So change your code to this:

from PIL import Image 

# Load image - ensuring RGB not palette
img=Image.open(r"C:\img1.png").convert('RGB')

pixelMap = img.load()
pixel = pixelMap[0,0]
print(pixel)

Upvotes: 1

Related Questions