Making all green areas of a PNG transparent

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
simplecarnival
Posts: 2
Joined: 2017-07-04T12:08:57-07:00
Authentication code: 1151

Making all green areas of a PNG transparent

Post by simplecarnival »

Hello --

I am trying to make all green areas of a PNG file transparent. That would include any pixels where the green is mixed with another color for anti-aliasing purposes. Here's what I initially tried:

Code: Select all

convert.exe "D:\junk\01_original.png" -colorspace RGB -separate -negate "D:\junk\02_mask.png"

convert.exe "D:\junk\01_original.png" -alpha Off "D:\junk\02_mask-1.png" -compose CopyOpacity -composite PNG32:"D:\junk\03_alpha.png"
This works almost as expected. However, I can see some light transparent green pixels around the edges of objects where there was anti-aliasing. I have seen solutions where the fuzz switch is used; this is an unacceptable solution because I want the input image to resemble the output image as closely as possible. I just want the green channel gone.

I have a workaround, which assumes that the final image is grayscale, but I would like to come up with a solution that will work with color images as well. Here's my workaround:

Code: Select all

convert.exe "D:\junk\01_original.png" -colorspace RGB -separate -negate "D:\junk\02_mask.png"

convert.exe "D:\junk\01_original.png" -alpha Off "D:\junk\02_mask-1.png" -compose CopyOpacity -composite PNG32:"D:\junk\03_alpha.png"

convert.exe "D:\junk\03_alpha.png" -colorspace Gray "D:\junk\04_alpha_grayscale.png"

convert.exe "D:\junk\04_alpha_grayscale.png" -colorspace RGB "D:\junk\05_final.png"
Again, this workaround is not ideal. How can I swap out all of the green with the appropriate level of transparency and not resort to using the fuzz switch?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making all green areas of a PNG transparent

Post by fmw42 »

Have you tried, just
convert image.png -backround none -flatten -compose over -alpha off -fuzz XX% -transparent green1 result.png
where XX is some small value to allow -transparent to work on colors close to green1. Note in ImageMagick green is not rgb(0,255,0). That rgb value in green1 or lime.

EDIT: Never mind, I see you do not want to use -fuzz.

Can you upload an example image to some free hosting service and put the URL here, so we can test with it.
simplecarnival
Posts: 2
Joined: 2017-07-04T12:08:57-07:00
Authentication code: 1151

Re: Making all green areas of a PNG transparent

Post by simplecarnival »

fmw42 wrote: 2017-07-04T12:44:42-07:00 Can you upload an example image to some free hosting service and put the URL here, so we can test with it.
Sure -- here you go: Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Making all green areas of a PNG transparent

Post by fmw42 »

Try one of these. I think perhaps you want the first one.

Code: Select all

convert \( 01_original.png -background none -flatten -alpha off \) \
\( -clone 0 -colorspace HSL -channel red -separate +channel -threshold 0 -negate -blur 0x0.5 -level 50x100% \) \
-alpha off -compose copy_opacity -composite result1.png

Code: Select all

convert \( 01_original.png -background none -flatten -alpha off \) \
\( -clone 0 -colorspace HSL -channel red -separate +channel -threshold 0 -negate -blur 0x0.5 -level 0x50% \) \
-alpha off -compose copy_opacity -composite result2.png
Do either work adequately?


EDIT: It looks like your original image has a totally opaque alpha channel. So in my command above the -background none -flatten is really not needed.

You get nearly the same thing as result1.png by doing just:

Code: Select all

convert \( 01_original.png -alpha off \)  \
\( -clone 0 -colorspace gray -threshold 0 -negate -blur 0x0.5 -level 50x100% \) \
-alpha off -compose copy_opacity -composite result3.png
or perhaps you want just:

Code: Select all

convert \( 01_original.png -alpha off \)  \
\( -clone 0 -colorspace gray -threshold 0 -negate \) \
-alpha off -compose copy_opacity -composite result4.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Making all green areas of a PNG transparent

Post by snibgo »

I think you want to do two things:

1. Make the green channel zero.

2. Copy the original green channel to alpha.

Is that right? As it happens, this image has zero in the red and blue channels, so (1) makes this image black, with transparency from (2). But here's a general solution:

Code: Select all

convert 01_original.png ( -clone 0 -channel G -evaluate set 0 +channel +write x0.png ) ( -clone 0 -channel G -negate -separate +channel +write x1.png ) -delete 0 -alpha off -compose CopyOpacity -composite x.png
snibgo's IM pages: im.snibgo.com
Post Reply