Dealing with alpha layers in TIFF->JPEG conversions
Dealing with alpha layers in TIFF->JPEG conversions
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
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
- 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
IM only supports one alpha channel as far as I know and JPEG does NOT support transparency (alpha channels) at all.
Re: Dealing with alpha layers in TIFF->JPEG conversions
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.fmw42 wrote:... and JPEG does NOT support transparency (alpha channels) at all.
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
-
- 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
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
(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
Re: Dealing with alpha layers in TIFF->JPEG conversions
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
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
- 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
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
convert image.tif -background white -flatten image.jpg
Re: Dealing with alpha layers in TIFF->JPEG conversions
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).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
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.
Re: Dealing with alpha layers in TIFF->JPEG conversions
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.
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.
Re: Dealing with alpha layers in TIFF->JPEG conversions
Your mask.tif image has two extra samples both of which are unspecified. The tiffinfo program reports:
- Extra Samples: 2<unspecified, unspecified>
- Extra Samples: 1<assoc-alpha>
-
- 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
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.
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
Re: Dealing with alpha layers in TIFF->JPEG conversions
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.
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.
Re: Dealing with alpha layers in TIFF->JPEG conversions
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.
That would help figure out whether to turn it off or not (using the -alpha Off) when using convert.
Re: Dealing with alpha layers in TIFF->JPEG conversions
For those of you using ImageMagick programatically:
Code: Select all
if (MagickGetImageProperty(w, "tiff:alpha") = "unassociated")
MagickSetImageAlphaChannel(w, DeactivateAlphaChannel)