Skip to content Skip to sidebar Skip to footer

Pil: Create One-dimensional Histogram Of Image Color Lightness?

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). Process each individual colu

Solution 1:

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

Solution 2:

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.

Post a Comment for "Pil: Create One-dimensional Histogram Of Image Color Lightness?"