Detect transparency

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
Liquix

Detect transparency

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect transparency

Post 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
Liquix

Re: Detect transparency

Post 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
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Detect transparency

Post 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
Liquix

Re: Detect transparency

Post by Liquix »

Im in depbt to you. Thank you man, you saved my day :)
Liquix

Re: Detect transparency

Post 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..
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Detect transparency

Post 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
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Detect transparency

Post 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
Liquix

Re: Detect transparency

Post 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 :)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Detect transparency

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply