Make area defined by Boolean mask 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
ExpertNoob
Posts: 6
Joined: 2011-02-06T08:09:27-07:00
Authentication code: 8675308

Make area defined by Boolean mask transparent

Post by ExpertNoob »

Hi,

Gereyscale "pic.tif" (8bit, 600dpi) and black & white "mask.tif" (1bit, 96dpi) are of the same size.
How can I make every black pixel in "mask.tif" transparent in "pic.tif" ? (pixel of the same coordinates, that is).

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

Re: Make area defined by Boolean mask transparent

Post by fmw42 »

one way is:

convert pic.tif mask.tif -alpha off -compose copy_opacity -composite result.tif

see http://www.imagemagick.org/Usage/compose/#copyopacity

Another is to use -compose dst_in, see http://www.imagemagick.org/Usage/compose/#dstin
ExpertNoob
Posts: 6
Joined: 2011-02-06T08:09:27-07:00
Authentication code: 8675308

Re: Make area defined by Boolean mask transparent

Post by ExpertNoob »

Thanks fmw42. I've been looking at the documentation for a while, to no avail.
With copy_opacity and dst_in, the white background pixels in pic.tif (which are black in mask.tif) remain white! They don't become transparent in result.tif.

I'm starting to believe IM can't do this simple task. I refuse to believe it!
Help!

Here are the files.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Make area defined by Boolean mask transparent

Post by fmw42 »

The command

convert pic.tif mask.tif -alpha off -compose copy_opacity -composite result.tif

or

convert pic.tif mask.tif +matte -compose copy_opacity -composite result.tif


works perfectly fine for me on IM 6.6.7.5 Q16 Mac OSX Tiger. The outer area of your drawing is made transparent.

What version of IM and what platform are you using? Perhaps an upgrade is in order.


identify -verbose result.tif
Image: result.tif
Format: TIFF (Tagged Image File Format)
Class: DirectClass
Geometry: 4054x5595+0+0
Resolution: 600x600
Print size: 6.75667x9.325
Units: PixelsPerInch
Type: GrayscaleMatte
Base type: Grayscale
Endianess: MSB
Colorspace: RGB
Depth: 8-bit
Channel depth:
gray: 8-bit
alpha: 1-bit
Channel statistics:
Gray:
min: 0 (0)
max: 255 (1)
mean: 252.175 (0.988921)
standard deviation: 16.2135 (0.0635825)
kurtosis: 72.105
skewness: -7.67387
Alpha:
min: 0 (0)
max: 255 (1)
mean: 13.8267 (0.0542222)
standard deviation: 57.7462 (0.226456)
kurtosis: 13.4999
skewness: -3.937
Alpha: rgba(255,255,255,0) #FFFFFF00

...
ExpertNoob
Posts: 6
Joined: 2011-02-06T08:09:27-07:00
Authentication code: 8675308

Re: Make area defined by Boolean mask transparent

Post by ExpertNoob »

IM v6.4.0 04/17/08 Q16 under WinXP SP3 up to date.
Just to make sure, I downloaded and installed latest version (v6.6.7-Q16) in a VM.
I get the same characteristics (identify -verbose) for "result.tif", but that's not transparent background!

Look at this screenshot from Photoshop:

Image

The small image (with text "Chapter One") has transparent background. The color info shows nothing for a pixel from its background, whilst it shows R=G=B=255 for a pixel from the background of "result.tif" (which is in line with the white background my eyes see!).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Make area defined by Boolean mask transparent

Post by fmw42 »

Photoshop uses two kinds of transparency, background and alpha. IM can read the background transparency from a TIFF or PSD, but converts it to its only form of transparency which is alpha. In the image command I gave you, the result has an alpha channel transparency and display just fine with other display devices. But PS will not see it as background transparency in its display. It only shows background transparency. But the alpha channel should be there in PS if you look.

However, most other display devices including browsers should display the transparency from the alpha channel properly as transparent.

Photoshop display is the oddball.
ExpertNoob
Posts: 6
Joined: 2011-02-06T08:09:27-07:00
Authentication code: 8675308

Re: Make area defined by Boolean mask transparent

Post by ExpertNoob »

Thanks for the explanation.
I think there is more to it: XnView seems to think the background is white, too !

Image

And FireFox, as well, displays a white background.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Make area defined by Boolean mask transparent

Post by anthony »

fmw42 wrote:one way is:

convert pic.tif mask.tif -alpha off -compose copy_opacity -composite result.tif

see http://www.imagemagick.org/Usage/compose/#copyopacity

Another is to use -compose dst_in, see http://www.imagemagick.org/Usage/compose/#dstin

For Dst_In you need to convert the black-white (greyscale) mask into a shape mask using -alpha copy or -alpha shape first. Both images should have alpha enabled for Dst_In.

convert pic.tif -alpha set \( mask.tif -alpha Copy \) -compose DstIn -composite result.tif

For most images the above is equivalent to CopyOpacity.

However if the original pic.tif, already has transparency the two methods can produce different results. CopyOpacity could for example make a transparent area of pic.tif opaque (and probably black). However DstIn will leave any transparent area in pic.tif, transparent.

Also if you want the other half, you can replace DstIn with DstOut instead. the two halves can be seamlessly restored by using 'Plus' or a image Blend.

Essentially no Duff-Porter composition method will ever make a transparent area 'visible'.
Because of this I have come to feel that DstIn is the better method, though CopyOpacity is still the simplest to understand (just replace alpha with the given greyscale image).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Make area defined by Boolean mask transparent

Post by fmw42 »

ExpertNoob wrote:Thanks for the explanation.
I think there is more to it: XnView seems to think the background is white, too !

Image

And FireFox, as well, displays a white background.

I know nothing about XnView and firefox has a white background so you cannot tell white from transparent. Perhaps XnView cannot view alpha transparency in a TIFF. Try converting to PNG or GIF instead of TIF

In IM, just

display result.png

and you should see transparent background.

I use Mac Preview and it shows transparent background around the central area.

Again, PS background transparency is different from alpha transparency, which IM converts to and uses.

Also use identify -verbose result.tif and you will see there is alpha channel transparency. I don't think many other tools will preserve or make transparency as background transparency rather than alpha transparency. PS and GIMP seem capable, but I know of no other tools. But I am not an expert on TIFF or PSD (Photoshop formats) background transparency vs its alpha transparency.

Where do you intend on displaying your resulting image? If not in PS, then it should not matter if the transparency is alpha. Most browsers will display either GIF or PNG with (alpha) transparency. They may not all work with TIFF transparency.
ExpertNoob
Posts: 6
Joined: 2011-02-06T08:09:27-07:00
Authentication code: 8675308

Re: Make area defined by Boolean mask transparent

Post by ExpertNoob »

Anthony: thanks for the explanation. I must admit I didn't get half of it :shock: but I'm sure it'll be useful to many who may face the same issue, and happen to be less ignorant than myself. :oops:
I tried your command, however, and it yielded a black background (as you predicted), which is, at least, easy on my eyes!
fmw42 wrote:Try converting to PNG or GIF instead of TIF
fmw42: I think you're exactly right.
I changed output format to PNG, and XnView, FireFox, .. all finally came to their senses, and behaved like PS in displaying transparent background. They don't handle TIFF properly, it seems. :-?

Thank you guys.
There are smart helpful people here around!
Post Reply