Skip to content
Advertisement

How to count the number of black and white pixels (linux, imagemagik, etc)

I have black and white images (see below). How count white and black pixels (as example 30% black and 70% white, or 123456 black pixels and 39393 white pixels)?

p.s. I work in linux, what i must use? imagemagick? i prefer a command line interface program.

sample

Advertisement

Answer

You can use ImageMagick’s histogram function to get a pixel count for each color in the image. Using your image as an example:

$ convert XPH7c.gif -define histogram:unique-colors=true 
> -format %c histogram:info:-
    153985: (  0,  0,  0,255) #000000 black
    182138: (255,255,255,255) #FFFFFF white

So, your image has 153985 black pixels, and 182138 white pixels.

Advertisement