Yoshinori Taguchi
Yoshinori Taguchi

Reputation: 21

I want to be able to identify the color of the image(Python3)

This image is divided into 3 colors, but I would like to be able to identify the color of the image in Python3. Please teach.

from PIL import Image
import PIL
import numpy as np


png_path = 'test.png'
img_array = np.asarray(Image.open(png_path))


Image.fromarray(img_array).show()
img_array = np.asarray(Image.open(png_path))[:,:,3]
print(np.unique(img_array))

##### I want to know the condition judgment method######
img_array_2 = np.where((img_array >=1) & (img_array <= 150), 0 , img_array)
##################

# check
imgPIL = Image.fromarray(img_array_2)
imgPIL.show()

Enter image descriptions here

Upvotes: 0

Views: 75

Answers (1)

ginchan
ginchan

Reputation: 11

Once you have the list in the color palette, it will be easier to separate them.

Image.getpalette() Returns the image palette as a list.

Returns: A list of color values [r, g, b, ...], or None if the image has no palette.

https://pillow.readthedocs.io/en/4.1.x/reference/Image.html#PIL.Image.Image.getpalette

Upvotes: 1

Related Questions