replace all one color with another including partial transp.

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
User avatar
rossmcm
Posts: 28
Joined: 2012-07-26T06:10:38-07:00
Authentication code: 15
Location: Auckland, NZ

replace all one color with another including partial transp.

Post by rossmcm »

I have a PNG image with a shape that is all one color, but the edges of the shape have partially transparent pixels where anti-aliasing has been used to smooth the edges. Image When I use the -fill option to replace all of the color by another

Code: Select all

convert 1.png -fill red -opaque black 2.png
only the pixels with 100% opacity are replaced, leaving the partially transparent edges with the original color. Image

How do I convert all of the pixels of that color, so that a partially transparent black pixel becomes a partially transparent red pixel.

This has to be a common requirement, but Google hasn't found anything for me.

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

Re: replace all one color with another including partial tra

Post by fmw42 »

First you background image is not all black, there are some white lines in it. To avoid problems of the white showing, this seems to work by making the whole background red.

convert 1.png -alpha off -fill red -colorize 100 -alpha on 4.png

The following also works if you want to keep your white lines in the background and just change the black parts to red

convert 1.png -alpha off -fill red -opaque black -alpha on 5.png
User avatar
rossmcm
Posts: 28
Joined: 2012-07-26T06:10:38-07:00
Authentication code: 15
Location: Auckland, NZ

Re: replace all one color with another including partial tra

Post by rossmcm »

Code: Select all

convert 1.png -alpha off -fill red -opaque black -alpha on 5.png
worked fine thanks. I'm baffled by your comment that the PNG contains white lines. I certainly can't see them with Gimp.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: replace all one color with another including partial tra

Post by fmw42 »

rossmcm wrote:

Code: Select all

convert 1.png -alpha off -fill red -opaque black -alpha on 5.png
worked fine thanks. I'm baffled by your comment that the PNG contains white lines. I certainly can't see them with Gimp.
convert 1.png -alpha off 1_aoff.png

That will remove the transparency so you can see the background image.


You can also see that from

convert 1.png -verbose info:

by looking at the histogram


idv 1.png
Image: 1.png
...
Histogram:
350: ( 0, 0, 0,255) #000000 black
325: ( 0, 0, 0, 0) #00000000 none
28: ( 0, 0, 0,159) #0000009F srgba(0,0,0,0.623529)
321: (255,255,255, 0) #FFFFFF00 srgba(255,255,255,0)


These are white pixels with full transparency.
User avatar
rossmcm
Posts: 28
Joined: 2012-07-26T06:10:38-07:00
Authentication code: 15
Location: Auckland, NZ

Re: replace all one color with another including partial tra

Post by rossmcm »

Aha!... I see now. A bit of image swarf. Many thanks for your assistance.
Post Reply