Page 1 of 1

How can I determine the visual, background color?

Posted: 2019-06-17T21:42:16-07:00
by josephmartin
I've got a program that users can upload images. The instructions say to always make the visual, background color 256 256 256; but they don't do so.

The images are simple street maps.

I'd like to be able to determine if the background is grey, light green, or whatever, and convert it to white.

Basically, I need the equivalent of the max bar on the histgram.

Anyone have a suggestion?

Re: How can I determine the visual, background color?

Posted: 2019-06-17T22:07:58-07:00
by fmw42
Please always provide your ImageMagick version and platform as syntax may vary. Also it is a good idea to provide what you have tried and an example image.

To determine the background color, one way is to simply measure it at some point that is background color, such as the top left corner, perhaps.

Code: Select all

convert image.suffix -format "%[pixel:u.p{0,0}]" info:
will return the color at the top left corner coordinate 0,0.

Then to change the color, you can either do a fuzzy replace of all colors similar to that everywhere or to flood fill the background starting at that point.

Code: Select all

convert image.suffix -fuzz XX% -fill white -opaque the_color_value result.suffix
or

Code: Select all

convert image.suffix -fuzz XX% -fill white -draw "color 0,0 flood fill" -alpha off result.suffix

Another way might be to find the dominant (most frequent) color in the histogram.


For novices, see

http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown
https://imagemagick.org/script/porting.php#cli