Replicating Gimp "Color To Alpha"

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
jbleach
Posts: 3
Joined: 2014-12-20T14:03:18-07:00
Authentication code: 6789

Replicating Gimp "Color To Alpha"

Post by jbleach »

Hi I was curious is anyone has any ideas on replicating how Gimp does "Color To Alpha" with IM. I can get close with a standard convert image.png -fuzz XX% -transparent white result.png, but Gimp is doing something additional with setting opacity or something on pixels that are left so it looks like you only have the color left with all of the white transparent. I have tried a couple things with setting white partially opaque but I can't seem to get the same affect as the Gimp "Color to Alpha".
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Replicating Gimp "Color To Alpha"

Post by dlemstra »

You should take a look at this stack overflow post: http://stackoverflow.com/questions/2640 ... a/26442085
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replicating Gimp "Color To Alpha"

Post by fmw42 »

This has been discussed and solved on the forum before, but I cannot find the link. I wrote a unix bash shell script to emulate it. See my script color2alpha at the link below.
jbleach
Posts: 3
Joined: 2014-12-20T14:03:18-07:00
Authentication code: 6789

Re: Replicating Gimp "Color To Alpha"

Post by jbleach »

Thanks for the replies, I searched and searched before posting and never found the stack overflow link you pointed me to. I will also try Fred's script again, when I tried before I couldn't get the affect I was seeing from Gimp. Since you are saying it is supposed to, I will try again and see what I was doing wrong. Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replicating Gimp "Color To Alpha"

Post by fmw42 »

I used the GIMP example on the referenced page to develop the script. If it fails for one of your images, send me the image and I will take a look. You can post the image to dropbox.com and put the URL here.
jbleach
Posts: 3
Joined: 2014-12-20T14:03:18-07:00
Authentication code: 6789

Re: Replicating Gimp "Color To Alpha"

Post by jbleach »

Ok, so the information from your replies gets most of the way there, but Gimp seems to be doing something additional to the remaining colors. I found that doing the following additional conversions gets me closer to what Gimp is doing for my needs. Posting in case it helps someone else.

//adjust colors
convert colortoalpharesult.png -colorspace sRGB -modulate 50,200,100 tmp.png

//make remaining colors semi-transparent
convert tmp.png -alpha set -channel A -evaluate Divide 1.2 tmp2.png
bomastudio
Posts: 2
Joined: 2016-01-22T02:16:03-07:00
Authentication code: 1151

Re: Replicating Gimp "Color To Alpha"

Post by bomastudio »

From the Gimp help
The Color to Alpha command makes transparent all pixels of the active layer that have a selected color. An Alpha channel is created. It will attempt to preserve anti-aliasing information by using a partially intelligent routine that replaces weak color information with weak alpha information. In this way, areas that contain an element of the selected color will maintain a blended appearance with their surrounding pixels.
So with an image with a white background and some gray areas how to replicate this behavior?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replicating Gimp "Color To Alpha"

Post by fmw42 »

As I wrote above:

This has been discussed and solved on the forum before, but I cannot find the link. So, I wrote a unix bash shell script to emulate it. See my unix bash script color2alpha at the link below.
bomastudio
Posts: 2
Joined: 2016-01-22T02:16:03-07:00
Authentication code: 1151

Re: Replicating Gimp "Color To Alpha"

Post by bomastudio »

I wrote a unix bash shell script to emulate it
I need to replicate it using ImageMagick only..... looking in the Gimp/src I found this

https://git.gnome.org/browse/gimp/tree/ ... d59edcb95d

Any way to do it with ImageMagick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replicating Gimp "Color To Alpha"

Post by fmw42 »

Just look at the two IM commands at the end of my script. The script is just a shell to pass arguments to the IM code. It is really only two lines of IM code with a conditional for what kind of output you want. Given any one output, the two commands could be combined into one command line.

If you do not like what my script (commands) produce, feel free to write the IM code from the GIMP code and contribute it to the IM community.
Post Reply