PIL Get the image palette.

I opened the image and converted it to an image with a 16-color palette using this code:

im = Image.open("SomeImage.png") im = im.convert("P") im = im.convert("P", palette = Image.ADAPTIVE, colors = 16) 

I can get pixel data using:

 im.getpixel((x,y)) 

Returns an integer corresponding to the color index in the palette. How can I get a palette as a list of colors?

+6
source share
2 answers

Your im has an im.palette attribute that you could use, but instead, it is recommended to use im.convert(mode) (you can omit the mode argument so that PIL selects it optimally) so that PIL searches for the palette internally on your behalf, much faster than you could.

+2
source

if you want the im.palette user im.palette get a palette from the image, the image must first have a palette.

I came across the same confusion with you, so I am writing a pip module called haishoku to capture the palette color and dominant colors from any image.

Haishoku

+1
source

Source: https://habr.com/ru/post/982838/


All Articles