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

后端 未结 2 707
醉酒成梦
醉酒成梦 2021-02-03 15:49

I\'ve been working on a script, and I need it to basically:

  • Make the image greyscale (or bitonal, I will play with both to see which one works better).
  • Pr
2条回答
  •  长情又很酷
    2021-02-03 16:39

    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,)
    

提交回复
热议问题