Convert one color to another including anti-aliased colors but keeping background

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
somebirdie
Posts: 1
Joined: 2015-03-23T06:32:23-07:00
Authentication code: 6789

Convert one color to another including anti-aliased colors but keeping background

Post by somebirdie »

Hi!

I have several png images (icons for my app) that have different colors and one main color (#1A96E5). I want to replace #1A96E5 everywhere to another color, red (#FF0000).

I do it using such command:
mogrify -path /mypath -format png -alpha off -fill "#FF0000" -opaque "#1A96E5" -alpha on *.png

It works perfectly for images with transparent background, but keeps blue shades around red image when background is not transparent (grey).
Here is an example. It's a source image:
Image

And that's a result:
Image

I understand why it worked that way. But is there any way to convert blue color to red in such images keeping gray background and keeping anti-aliased edges?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert one color to another including anti-aliased colors but keeping background

Post by snibgo »

Your links don't work.

"-fuzz" might help. However, "-opaque" is a binary operator. Each pixel is either changed to the new colour, or it isn't. You might need something more sophisticated.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert one color to another including anti-aliased colors but keeping background

Post by snibgo »

Your images now show.

We have two colours that are blended, and we want to replace the second colour with a new one, with correct blending. Windows BAT syntax:

Code: Select all

convert ^
  blue-source.png ^
  ( -clone 0 -alpha extract +write mpr:ALPH +delete ) ^
  -alpha off ^
  ( -clone 0 -fill rgb(237,239,242) -colorize 100 +write mpr:C1 ) ^
  ( -clone 0 -fill rgb(26,150,229)  -colorize 100 +write mpr:C2 ) ^
  ( -clone 1-2 -compose MinusSrc -composite ) ^
  ( -clone 0,2 -compose MinusSrc -composite ) ^
  -delete 0-2 ^
  -compose Divide -composite ^
  mpr:C1 ^
  ( +clone -fill rgb(100%%,0,0) -colorize 100 ) ^
  -swap 0,2 ^
  -compose Over -composite ^
  mpr:ALPH ^
  -alpha off -compose CopyOpacity -composite ^
  red-arrow.png
There may be an easier way.
Image
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert one color to another including anti-aliased colors but keeping background

Post by snibgo »

Yes, I knew there was an easier way:

Code: Select all

convert ^
  blue-source.png ^
  -level-colors rgb(237,239,242),rgb(26,150,229) ^
  +level-colors rgb(237,239,242),rgb(100%%,0,0) ^
  ra.png
snibgo's IM pages: im.snibgo.com
Post Reply