Python method to get color information for an image

  • 2020-04-02 14:40:42
  • OfStack

This article illustrates how python gets color information for images. Share with you for your reference. Specific analysis is as follows:

Python's pil module can get the color information for each pixel of an image from an image, and the following code shows how to get the color information for all the points in the image and the number of each color.


from PIL import Image
image = Image.open("jb51.gif")
image.getcolors()

The return result is as follows

..., (44, (72, 64, 55, 255)), (32, (231, 208, 141, 255)), (2368, (70, 64, 55, 255)),
(1, (187, 210, 216, 255)), (256, (68, 64, 57, 255)), (592, (67, 80, 103, 255)),
(2, (198, 204, 214, 255), ...

 
The result is A primitive, each element in the following format: (44, (72,64,55,255)), where (72,64,55,255) represents the RGBA color, A is transparency, and 44 represents jb51.gif.

I hope this article has helped you with your Python programming.


Related articles: