Dealing with alpha layers in TIFF->JPEG conversions

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
martinw17
Posts: 39
Joined: 2006-09-07T02:10:27-07:00
Location: Brighton, UK

Dealing with alpha layers in TIFF->JPEG conversions

Post by martinw17 »

I am trying to figure out how to convert various TIFF files to JPEGs using one IM command. The problem is that the TIFFs were created with Photoshop and some contain an alpha channel representing transparency while others contain one of more alpha channels (I think) storing mask/selection information.
If possible, I would like any transparency to be converted to white in the resulting JPEG, and selection/mask information to be discarded.

For example, this TIFF: http://www.bright-interactive.com/downl ... s/test.tif has a transparent background.
If I convert to a JPEG like this:
convert test.tif test.jpg
then the resulting image has a black background
I get a white background using:
convert test.tif -layers flatten test.jpg

However, this TIFF: http://www.bright-interactive.com/downl ... s/mask.tif
contains a selection mask.
convert mask.tif mask.jpg
gives a good result - the alpha channel seems to be ignored for a simple convert.

However, if I resize:
convert mask.tif -resize 100x100 mask.jpg
I get a black background (i.e. I think resize is considering the alpha channel, unlike convert with no options).

I can get round this by explictly discarding the alpha channel(s):
convert mask.tif -alpha Off -resize 100x100 mask.jpg
This gives a good result.

However, if I try to come up with one command that works for both TIFFs, then the first TIFF is converted with a black background:
convert test.tif -alpha Off -flatten -resize 100x100 test.jpg

Is it possible to process both of these TIFFs with the same command so that both resulting JPEGs look acceptable?
If not, is it possible to figure out using ImageMagick (for example using identify) if an alpha channel created by Photoshop is a mask rather than transparency? (So I can then use different options with convert?)

Any help/advice would be much appreciated!

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

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by fmw42 »

IM only supports one alpha channel as far as I know and JPEG does NOT support transparency (alpha channels) at all.
martinw17
Posts: 39
Joined: 2006-09-07T02:10:27-07:00
Location: Brighton, UK

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by martinw17 »

fmw42 wrote:... and JPEG does NOT support transparency (alpha channels) at all.
I realise that - that's why I want to figure out how to convert an alpha channel used for transparency to a white background (when converting to JPEG) but discard an alpha channel used for Photoshop selections. However, I don't think it's possible for IM (and perhaps anything else other than Photoshop) to make the distinction.

I still don't fully understand why IM ignores the alpha channel when doing a convert from TIFF to JPEG with no options but uses it when you specify the resize flag. When using 'convert' with or without resize I would expect to see the same image (only differently sized).

Regards,
Martin
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by snibgo »

I don't know the answers, but:

(a) The Ubuntu file manager (Nautilus) seems to give the behaviour you want

(b) So does Gimp, although opening mask.tif gives the warning: "TIFF image Message. alpha channel type not defined for file mask.tif. Assuming alpha is not premultiplied."

So I assume the files contain something that distinguishes the cases.

Running:
identify -verbose test.tif >test.txt
identify -verbose mask.tif >mask.txt

may give the answer. In particular, test.txt contains:
Type: TrueColorMatte
Base type: TrueColor
Alpha: none #00000000
tiff:alpha: associated

where mask.txt contains:
Type: ColorSeparationMatte
Base type: ColorSeparation
Alpha: cmyka(185,155,247,148,0) #B99BF79400
tiff:alpha: unassociated

One of these might (or might not) be what you (or ImageMagick) need to test for. You might want to read TIFF documentation about that associated/unassociated flag.

See http://www.exif.org/TIFF6.pdf
snibgo's IM pages: im.snibgo.com
martinw17
Posts: 39
Joined: 2006-09-07T02:10:27-07:00
Location: Brighton, UK

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by martinw17 »

Thanks - that's very helpful.

I'm also exploring using the fact that Photoshop seems to use the XMP field Photoshop:AlphaChannelsNames to store the names of channels used to store selections, and one called 'Transparency' if it uses an alpha channel to store transparency information.

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

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by fmw42 »

to convert an image with an alpha channel such as png or tiff to white background as jpg, try

convert image.tif -background white -flatten image.jpg
martinw17
Posts: 39
Joined: 2006-09-07T02:10:27-07:00
Location: Brighton, UK

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by martinw17 »

fmw42 wrote:to convert an image with an alpha channel such as png or tiff to white background as jpg, try

convert image.tif -background white -flatten image.jpg
Yes - you'll see from my initial post that this is the command I use to convert a TIFF containing a transparent background to a JPEG with a white background (although as 'white' is the default, I skipped the 'background' option).

The issue I am trying to solve is, for TIFFs created in Photoshop, how to differentiate between an alpha channel added to store transparency and one or more added to save selections.
martinw17
Posts: 39
Joined: 2006-09-07T02:10:27-07:00
Location: Brighton, UK

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by martinw17 »

Here's what I have discovered so far.
Photoshop stores transparency in an 'associated' alpha channel and selections in 'unassociated' alpha channels, as per the TIFF spec.
If a TIFF has both transparency and selections, it looks like Photoshop always stores the associated alpha channel first. From what I can tell, ImageMagick recognises only one alpha channel. So, if a TIFF saved from Photoshop contains both associated and unassociated alpha channels the the alpha channel recognised by IM will be the associated one.
This means I could use identify -verbose to see if tiff:alpha is set to 'associated' or 'unassociated' (as suggested by snibgo - thanks!) - if it is associated then we need it, if unassociated then it can be discarded with the -alpha Off option.

However, shouldn't 'convert' distinguish between associated and unassociated alpha channels when converting to formats that don't support alpha channels (like JPEG)? It would seem reasonable to ignore unassociated alpha channels.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by magick »

Your mask.tif image has two extra samples both of which are unspecified. The tiffinfo program reports:
  • Extra Samples: 2<unspecified, unspecified>
How can ImageMagick determine which is associated alpha and which is not? The TIFF spec permits these samples to be marked as associated or unassociated alpha. We provide the tiff:alpha define for this reason, to mark an unspecified extra sample as associated or unassociated alpha. Perhaps we need to expand the definition to permit the user to specify which extra sample is associated or unassociated alpha. The test.tif image does the right thing. It tells ImageMagick its associated alpha:
  • Extra Samples: 1<assoc-alpha>
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by snibgo »

Thanks, magick.

There is also the question of why

convert mask.tif mask.jpg
convert mask.tif -resize 100x100 mask.jpg


do two different things with alpha.
snibgo's IM pages: im.snibgo.com
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by magick »

We added a patch such that if the extra samples are unspecified, no alpha channel is activated.

The resize option behaves differently because it blends the alpha channel with the RGB pixels. If there is a bogus alpha channel, unpredictable results are possible.
martinw17
Posts: 39
Joined: 2006-09-07T02:10:27-07:00
Location: Brighton, UK

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by martinw17 »

How about extending the -format options available to identify so it is possible to find out if an alpha channel is 'unspecified', 'unassociated' or 'associated'? Perhaps the current %A option could return one these three values (in place of 'True') or 'False' (if there's no alpha channel, i.e. as it does now).

That would help figure out whether to turn it off or not (using the -alpha Off) when using convert.
sacha
Posts: 17
Joined: 2012-04-12T14:37:19-07:00
Authentication code: 8675308

Re: Dealing with alpha layers in TIFF->JPEG conversions

Post by sacha »

For those of you using ImageMagick programatically:

Code: Select all

    if (MagickGetImageProperty(w, "tiff:alpha") = "unassociated")
        MagickSetImageAlphaChannel(w, DeactivateAlphaChannel)
Post Reply