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

后端 未结 2 708
醉酒成梦
醉酒成梦 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,)
    
    0 讨论(0)
  • 2021-02-03 16:43

    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.

    0 讨论(0)
提交回复
热议问题