PIL: Create one-dimensional histogram of image color lightness?

核能气质少年 提交于 2019-12-02 21:29:00

I see you are using numpy. I would convert the greyscale image to a numpy array first, then use numpy to sum along an axis. Bonus: You'll probably find your smoothing function runs a lot faster when you fix it to accept an 1D array as input.

>>> from PIL import Image
>>> import numpy as np
>>> i = Image.open(r'C:\Pictures\pics\test.png')
>>> a = np.array(i.convert('L'))
>>> a.shape
(2000, 2000)
>>> b = a.sum(0) # or 1 depending on the axis you want to sum across
>>> b.shape
(2000,)

From the docs for PIL, histogram gives you a list of pixel counts for each pixel value in the image. If you have a grayscale image, there will be 256 different possible values, ranging from 0 to 255, and the list returned from image.histogram will have 256 entries.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!