[SOLVED] Replace existing colour with a transparent colour

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
gubi
Posts: 3
Joined: 2017-08-22T15:52:28-07:00
Authentication code: 1151

[SOLVED] Replace existing colour with a transparent colour

Post by gubi »

Hi all,
I have a transparent png file and I want to replace an existing opaque colour (eg. #000) with a transparent rgba colour (eg. rgba(255, 0, 0, 50.0)). Is it possible?
I spent days without result.
Here the command I use:

Code: Select all

convert -fuzz 100% img_black.png -alpha on -fill 'rgba(255, 0, 0, 50.0)' -opaque '#000' image_red.png
Please, anyone can help me?
Thanks in advance
Last edited by gubi on 2017-08-23T13:56:01-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace existing colour with a transparent colour

Post by fmw42 »

try reading the input first, then use opaque black. Also alpha is specified in the range of 0 to 1, not 0 to 100 or 0 to 255. So if you want 50% transparent use 0.5

Code: Select all

convert img_black.png -fuzz 100% -alpha on -fill 'rgba(255, 0, 0, 0.5)' -opaque '#000' image_red.png
or if that does not work, then

Code: Select all

convert img_black.png -fuzz 100% -alpha on -fill 'rgba(255,0,0,0.5)' -opaque '#000000FF' image_red.png
Please always provide your IM version and platform when asking questions on this forum.
gubi
Posts: 3
Joined: 2017-08-22T15:52:28-07:00
Authentication code: 1151

Re: Replace existing colour with a transparent colour

Post by gubi »

Hi,
thanks for your reply.
First of all, sorry for my forgetfulness.

Code: Select all

$ convert -version
Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib
My OS is Ubuntu Linux 16.04

I tried your commands simply by copy-paste but without success.
This is the black image:
Image

This is the output of the fist command:

Code: Select all

convert img_black.png -fuzz 100% -alpha on -fill 'rgba(255, 0, 0, 0.5)' -opaque '#000' image_red.png
Image

And this is the output of the second command:

Code: Select all

convert img_black.png -fuzz 100% -alpha on -fill 'rgba(255,0,0,0.5)' -opaque '#000000FF' image_red.png
Image

I expecting something like this:
Image

Important: I opened the output image with Gimp and the color profile is "indexed...". Is it possible to assign an RGB color profile to the output?

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

Re: Replace existing colour with a transparent colour

Post by fmw42 »

Your image is totally black with a binary alpha channel. So one way is to make it totally red with an alpha channel reduced by 50%. This works for me on IM 6.9.9.8 Q16 Mac OSX. Change my path to your path to your sRGB.icc profile

Code: Select all

convert img_black.png -alpha off -fill red -opaque black -alpha on -channel a -evaluate multiply 0.5 +channel -profile /Users/fred/images/profiles/sRGB.icc image_red_half_alpha.png
Image

This also works. Add -channel rgba from my earlier suggested command.

Code: Select all

convert img_black.png -alpha on -channel rgba -fill 'rgba(255,0,0,0.5)' -opaque '#000000FF' -profile /Users/fred/images/profiles/sRGB.icc image_red_half_alpha.png
gubi
Posts: 3
Joined: 2017-08-22T15:52:28-07:00
Authentication code: 1151

Re: Replace existing colour with a transparent colour

Post by gubi »

Thanks!
The first command works as well.
The colour profile is downloadable from here: http://archive.ubuntu.com/ubuntu/pool/m ... ig.tar.bz2

Thanks a lot.
Gubi
Post Reply