Skip to content Skip to sidebar Skip to footer

Understanding Histogram() In Pillow

From the docs: im.histogram() => list Returns a histogram for the image. The histogram is returned as a list of pixel counts, one for each pixel value in the source image.

Solution 1:

I haven't tested, but from the documentation, the wording seems to indicate that the histogram only applies to each channel (e.g. Red, Green, Blue) individually.

If the image has more than one band, the histograms for all bands are concatenated (for example, the histogram for an “RGB” image contains 768 values).

So, no, the examples you gave aren't really correct. 768 values is just 256 * 3, which is the number of possible red values, plus the number of possible green values, plus the number of possible blue values independently. It does not represent all possible combinations of red, green, and blue, which would instead be 256 ^ 3 == 16777216.

From what I can see, the interpretation of your example histogram values should be:

329  pixels with value of R = 0, G = ?, B = ? and
145  pixels with value of R = 1, G = ?, B = ? and
...
460  pixels with value of R = ?, G = 1, B = ? and
...
3953 pixels with value of R = ?, G = ?, B = 256

Solution 2:

No, you don't know how many pixels had (e.g.) R=0, G=0, B=0. That would require a histogram with something like 16 million entries.

You only know how many had R=0, how many had G=0, and how many had B=0, taken independently.

It would be entirely possible for the R=0 pixels to have a great many different amounts of G and B.


Solution 3:

if the mode is RGB to open a image, 768 mean is Red grey list[0-255 pixs nums]+Green grey list[0-255 pix nums]+ Blue grey list[0-255 pix numbs]

im is a Image object by RGB

In [59]: x = reduce(lambda x,y:x+y,im.histogram())

In [60]: x Out[60]: 47191725

In [61]: im.size Out[61]: (4731, 3325)

In [62]: 4731*3325 Out[62]: 15730575

In [63]: 4731*3325*3 Out[63]: 47191725

In [64]:

or other way:

r, g, b = im.split()

len(r.histogram()) = 256

len(g.histogram()) = 256

len(b.histogram()) = 256


Post a Comment for "Understanding Histogram() In Pillow"