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


Here is what I have achieved:


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


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?

2 Answers 2


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


  • 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.


The online manual meanwhile (2022-06-27) has a whole chapter on Replacing Colors in Images showcasing a variety of use cases with all the options explained in code and with before/after illustrations.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .