Page 1 of 1

### Get amount of white(-ish) pixels in image(PNG)

Posted: 2015-08-27T00:28:40-07:00
Hi,
what is the best way to get the amount of pixels that fall in a certain RGB range. I'm looking for a way to determine how much whitespace my image has. For starters it would be enough to measure the really white pixels (255,255,255) - but then I need to measure a range, let's say from (200,200,200)to(255,255,255).

### Re: Get amount of white(-ish) pixels in image(PNG)

Posted: 2015-08-27T03:26:23-07:00
To count the number of pixels that are exactly (or approximately) any given colour, I would:

1. Turn that colour white. Eg "-fuzz 5% -fill White -opaque {colour}"

2. Turn all other pixels black. "-fill Black +opaque White"

3. Find the mean. This is the proportion of pixels that are now white. "-format %[fx:mean] info:" Multiply by the number of pixels. "-format %[fx:int(mean*w*h+0.5)] info:"

Finding colour in a range is trickier. Do you mean:

200<=red<=255 AND 200<=green<=255 AND 200<=blue<=255 ?

"-fx" is probably the neatest answer. But it is slow.

### Re: Get amount of white(-ish) pixels in image(PNG)

Posted: 2015-08-27T06:03:58-07:00
Thank you for the quick answer, snibgo.

So I did this

Code: Select all

``````convert 50889165.png -fuzz 4% -fill White -opaque White out.png
convert out.png -fuzz 4% -fill Black +opaque White out.png
``````
which gives me a black and white image -great!

Code: Select all

``convert out.png  -format %[fx:int(mean*w*h+0.5)] info:``
Gives me an output of 4.13491e+006 . How do I interprete this number?

Yeah I mean I need to find pixels that are not 100% pure white but kind of. This could happen if a white sheet of paper got scanned in for example

### Re: Get amount of white(-ish) pixels in image(PNG)

Posted: 2015-08-27T06:31:30-07:00
The mean is a number from 0.0 to 1.0. This is the proportion that is white. For example, a result of 0.25 would mean that 25% of pixels were white.

When you multiply this by the number of pixels in the image, which is width*height, the result is the number of pixels that were white.

4.13491e+006 is about 4.1 million. That's the number of pixels that were white. Use "-precision 15" to write it as an ordinary integer.

I would do the whole thing in a single convert:

Code: Select all

``````convert 50889165.png -fuzz 4% -fill White -opaque White -fuzz 0 -fill Black +opaque White -precision 15 -format %[fx:int(mean*w*h+0.5)] info:
``````

### Re: Get amount of white(-ish) pixels in image(PNG)

Posted: 2015-08-27T06:47:45-07:00
That's brilliant and it is a big help to my solution - thank you very much snibgo!