Page 1 of 1

Detect transparency

Posted: 2008-08-06T08:59:21-07:00
by Liquix
Ive got an image. I want to modify this image and create a mask so that all transparent areas get for instance red, and all areas without transparency(Anything from white to red) gets for instance blue.. How is it possible to do this?

Re: Detect transparency

Posted: 2008-08-06T12:07:34-07:00
by fmw42
What you are asking is not clear enough as saying everything from white to red should go to blue is vague as to what that really means. However, if by that you mean make the red channel black (so it goes away) and put the old red channel into the blue, then try this:

convert logo2t.png \
\( -clone 0 -channel rgba -separate \) \
\( -clone 0 -channel r -fx "black" \) \
-delete 0 \
-swap 0,2 -swap 0,4 -delete 4 -channel rgba -combine \
-fill red -opaque none \
logo2t_finish.png

second line: separate all channel r,g,b,a
third line: create black image size of original
fourth line: delete original
fifth line: swap red with blue, swap black with new red (now black), delete old red, recombine all channels
sixth line: make any transparent areas red

original image (note transparent on light gray background will be light gray):
Image

here is the result:
Image

Re: Detect transparency

Posted: 2008-08-08T12:55:10-07:00
by Liquix
What I want to do is create a mask. and the mask is gonna be like this:
Ive got an image. Let's say this:
Image

I want all transparent areas to go "#FF00FF" and the rest to go transparent. It would produce this result:
Image

Or as I asked in the first post, this can be an alternaive:
Image

The basic idea of why I want this is because I got a watermark which I only want to place on non-transparent spots. So by creating this mask and overlaping it onto the watermark, I will prevent the watermark from going on transparent spots. If there is any easier ways, please tell me :)

I use #FF00FF as transparent areas on watermark and #FFFFFF as colored areas. So by overlaping a mask of the original image's transparency where transparent areas are replaced with #FF00FF and all other areas with transparency(any color can act as transparency druing merging). I will remove any white areas from the watermark where the transparency is.

Watermark + Mask
Image + Image = Image

Re: Detect transparency

Posted: 2008-08-08T13:50:41-07:00
by el_supremo
This produces the first version with the transparent background

Code: Select all

convert logo2t.png -channel a -separate +channel  -fill #FF00FF -opaque white -transparent black logo2t_trans.png
and this one makes a blue background

Code: Select all

convert logo2t.png -channel a -separate +channel  -fill #FF00FF -opaque white -fill blue -opaque black logo2t_blue.png
Pete

Re: Detect transparency

Posted: 2008-08-08T13:53:16-07:00
by Liquix
Im in depbt to you. Thank you man, you saved my day :)

Re: Detect transparency

Posted: 2008-08-08T14:46:05-07:00
by Liquix
On second thoughts:

It works perfectly on my computer! (ImageMagick 6.4.2 Q16)
But on my webpage it doesnt do anything! (ImageMagick 6.3.6)

Does the version of imagemagick got to do something with this or is it someting lse which is wrong? The code doesnt output any error messages either..

Re: Detect transparency

Posted: 2008-08-08T14:47:49-07:00
by fmw42
Thanks Pete!

It would have been nice if he had made it clear exactly what he wanted to do and not made some vague question.

Fred

Re: Detect transparency

Posted: 2008-08-08T18:20:08-07:00
by el_supremo
Liquix wrote:But on my webpage it doesnt do anything! (ImageMagick 6.3.6)
As far as I know, the command should work with IM6.3.6. I don't know why it won't work on your webpage.

Pete

Re: Detect transparency

Posted: 2008-08-09T08:29:43-07:00
by Liquix
I found out.. :)

Had to use this code instead:

Code: Select all

convert logo2t.png -channel a -separate +channel  -fill "#FF00FF" -opaque white -transparent black logo2t_trans.png
Seems the " is required on some enviroments :)

Re: Detect transparency

Posted: 2008-08-11T00:13:49-07:00
by anthony
Liquix wrote:Ive got an image. I want to modify this image and create a mask so that all transparent areas get for instance red, and all areas without transparency(Anything from white to red) gets for instance blue.. How is it possible to do this?
An alternative for solid colors is...

Code: Select all

    convert image.png  -fill blue  -colorize 100% \
                    -background red -flatten   colored_mask.png
This and all other methods are listed in IM Examples, Channels, Masks, and transparency
http://www.imagemagick.org/Usage/channels/#extract