How to replace colors (shape color and background color)

colorsimage editingimagemagick

I want to change the white background to #3D94DB (Hex triplet notation) and change the black lines to white. The image is as following:

O

Here is what I have achieved:

One

convert input.png +level-colors red, output.png

I changed the color of the lines to red. So white is easy.

How how can I change both colors?

Reading from imagemagick: color_basics

convert balloon.gif   -alpha set  -channel RGBA \
                    -fill none -opaque blue   balloon_none.gif

So here is my code:

 convert input.png +level-colors gray, output.png
 convert output.png -fill "#3D94DB" -opaque white target.png

result

And turn it upside down, the result is embarrassing.

Using Photoshop is easy but I have many icons to handle.

Can you please show me a better solution?

Best Answer

I want to change the white background to #3D94DB (Hex triplet notation) and change the black lines to white.

As you may have already noticed, there are actually subtle gradations (from black to white) of the "black" lines in your example icon (likely due to anti-aliasing). These need to remain intact to help maintain the look of the icon:

original icon with gradations

Per the Imagemagick usage guide on Level Adjustment by Color, +level-color will help both change the "black" and "white" colors, as well as (roughly) give the proper color gradations:

convert input.png +level-colors white,"#3D94DB" target.png

With your example icon, this command produces the following results:

target icon results - white on blue


Caveats:

  • In the command above, the replacement color order is for black then white.

  • There should be no spaces between the replacement colors or it will cause the command to fail.

  • Given the desired effect, the command only works for black and white images. Images with other colors will produce unexpected results. Likewise, two-color images work best if you convert a color image to grayscale.


Related Question