How can I determine the visual, background color?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
josephmartin
Posts: 2
Joined: 2019-06-17T21:41:08-07:00
Authentication code: 1152

How can I determine the visual, background color?

Post by josephmartin » 2019-06-17T21:42:16-07:00

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?

User avatar
fmw42
Posts: 25664
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 » 2019-06-17T22:07:58-07:00

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

Post Reply