Page 1 of 1

Make area defined by Boolean mask transparent

Posted: 2011-02-06T08:27:31-07:00
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.

Re: Make area defined by Boolean mask transparent

Posted: 2011-02-06T11:56:07-07:00
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

Re: Make area defined by Boolean mask transparent

Posted: 2011-02-06T13:26:48-07:00
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.

Re: Make area defined by Boolean mask transparent

Posted: 2011-02-06T13:57:43-07:00
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

...

Re: Make area defined by Boolean mask transparent

Posted: 2011-02-06T15:01:13-07:00
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!).

Re: Make area defined by Boolean mask transparent

Posted: 2011-02-06T15:17:18-07:00
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.

Re: Make area defined by Boolean mask transparent

Posted: 2011-02-06T15:23:51-07:00
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.

Re: Make area defined by Boolean mask transparent

Posted: 2011-02-06T21:43:27-07:00
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).

Re: Make area defined by Boolean mask transparent

Posted: 2011-02-06T22:11:17-07:00
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.

Re: Make area defined by Boolean mask transparent

Posted: 2011-02-07T07:13:28-07:00
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!