ImageMagick count how many identical or almost identical color pixels are on the edges left and right of image

Viewed 70

This is an example PNG image:

enter image description here

it's 50x40 but enlarged here to see it better.

The hex of bottom left and bottom right pixels is F9E4B7

I need to know how many pixels there are on vertical edges from the bottom left and right with almost this color, like 1% fuzz because they are not all exactly F9E4B7 but some are very slightly different.

In this case it would be 20px of each side:

I'll mark the areas which need to be counted just to make it perfectly clear:

enter image description here

I also need to be able to count how many pixels are exactly F9E4B7, which in this case would be 15 left and 20 right, I'll mark the slightly different areas so it's clear where the counter needs to stop:

enter image description here

1 Answers

The way to do that in Imagemagick is to use fuzz to change all your pixels close to that color to white and the rest black. The crop the first or last column and use -fx to get the count from mean*width*height. So for example for the left full column of the whole image, one has

Unix Imagemagick 6 syntax:

convert tan.png -alpha off \
-fuzz 1% -fill white -opaque "#F9E4B7" -fill black +opaque white \
-gravity west -crop 1x+0+0 -format "%[fx:round(mean*w*h)]\n" info:

Windows Imagemagick Syntax:

convert tan.png -alpha off ^
-fuzz 1% -fill white -opaque "#F9E4B7" -fill black +opaque white ^
-gravity west -crop 1x+0+0 -format "%[fx:round(mean*w*h)]\n" info:

Result:

108

To get the right side, use -gravity east.

For exact count, use -fuzz 0

For Imagemagick 7, change convert to magick

Related