简体   繁体   中英

Output the number of each RGB value of an mask image using pillow?

I am trying to extract the number of each RGB value from the sample photo like below using python pillow. But, the image is only processing the background color Green.

示例图像

I have tried this script

i = Image.open(img_path, 'r')
r, g, b = i.getpixel((0, 0))
print("Red: {}, Green: {}, Blue: {}".format(r, g, b))

But the output is showing like this.

Red: 0, Green: 255, Blue: 0

the image size is (2048, 1536) and its PNG. How do I get value of red and blue pixels? I am new to this any kind of help is appreciate.

`

You can get your unique RGB values by:

np.unique(np.array(i).reshape(-1,3),axis=0)

However, your image has more than simple green/blue/red points:

Unique sets of RGB in your image:

[[  0  42 255]
 [  0  44 255]
 [  0  46 193]
 ...
 [255  64  44]
 [255  64  63]
 [255  65  44]]

or:

print(["Red: {}, Green: {}, Blue: {}".format(r, g, b) for (r,g,b) in colors])

['Red: 0, Green: 42, Blue: 255', 
'Red: 0, Green: 44, Blue: 255', 
'Red: 0, Green: 46, Blue: 193', 
'Red: 0, Green: 46, Blue: 207', 
'Red: 0, Green: 47, Blue: 215', 
'Red: 0, Green: 50, Blue: 255', 
'Red: 0, Green: 53, Blue: 255', 
'Red: 0, Green: 83, Blue: 90', 
'Red: 0, Green: 94, Blue: 100', 
'Red: 1, Green: 45, Blue: 238',
...
]

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