Inconsistent alpha flattening of transparent TIFs

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
quicksnap
Posts: 5
Joined: 2015-08-06T21:34:40-07:00
Authentication code: 1151

Inconsistent alpha flattening of transparent TIFs

Post by quicksnap »

Hello! I'm trying to do this with the C API (not C++), but I'm able to replicate this problem with "convert".

I am trying to consistently flatten TIFFs against a white background. When I encounter a TIFF that has transparency disabled, but contains an auxiliary alpha channel, Image Magick considers that channel the transparency channel, and results are not good.

I have two input TIFF files--one with transparency enabled, one with it disabled. Both have a checkerboard background in Photoshop. I am using the "Enable Transparency" in the Save As dialog within Photoshop.

Both files have an additional channel added within Photoshop containing some brush strokes of leaves.

The sample files are here:

Without Transparency: https://www.dropbox.com/s/fli26u0u4f25i ... y.tif?dl=0
With Transparency: https://www.dropbox.com/s/s1aidyct4oqib ... .tif?dl=0z

Here are the commands I run against them:

Code: Select all

convert with-transparency.tif -background white -alpha off -flatten out/whiteAlphaOffFlatten.tif
convert with-transparency.tif -background white  -flatten out/whiteFlatten.tif
convert without-transparency.tif -background white  -flatten out/without-whiteFlatten.tif
convert without-transparency.tif -background white -alpha off -flatten out/without-whiteAlphaOffFlatten.tif
And these are the output results: https://dl.dropboxusercontent.com/spa/4 ... 5rm_ae.png

I would love some input on how I can flatten these suckers! Am I doing something wrong? This is driving me bonkers!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Inconsistent alpha flattening of transparent TIFs

Post by fmw42 »

Both your files are the same. They both have a disabled alpha channel when I open them in PS (CS), though that may not be how they were saved. Both also have background transparency. Probably, IM can only deal with one or the other, as far as I know, unless there is a new -define to select which one. They are also both CMYK (with USWebCoatedSwop.icc profile) and not RGB. I would convert to RGB using profiles before flattening.

These work for me in IM 6.1.9.10 Q16 Mac OSX Snow Leopard

Code: Select all

convert with-transparency.tif -profile /users/fred/images/profiles/sRGB.icc -background white -compose over -flatten with_flattened.tif

convert without-transparency.tif -profile /users/fred/images/profiles/sRGB.icc -background white -compose over -flatten without_flattened.tif
quicksnap
Posts: 5
Joined: 2015-08-06T21:34:40-07:00
Authentication code: 1151

Re: Inconsistent alpha flattening of transparent TIFs

Post by quicksnap »

Thanks for your help! I tried your command, and it didn't work.. however, I was using an older version of Image Magick (6.8.9), and I tested it against 6.9.1-10, and it works on that version for both files! Time to rev..

Thanks again!
quicksnap
Posts: 5
Joined: 2015-08-06T21:34:40-07:00
Authentication code: 1151

Re: Inconsistent alpha flattening of transparent TIFs

Post by quicksnap »

You wouldn't happen to know the C API calls to replicate your convert command above, do you?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Inconsistent alpha flattening of transparent TIFs

Post by fmw42 »

Sorry, I do not know any of the IM APIs only command line.
quicksnap
Posts: 5
Joined: 2015-08-06T21:34:40-07:00
Authentication code: 1151

Re: Inconsistent alpha flattening of transparent TIFs

Post by quicksnap »

Once again, I'm at an impasse. Consider the following TIF: https://www.dropbox.com/s/usn9jv0protgijv/bad.tif?dl=0

When I run the following command, trying to convert it to a JPEG, it chokes:

Code: Select all

convert bad.tif -profile ./sRGB.icc -background white -compose over -flatten ugly.jpg
The problem is that this TIFF contains an alpha channel but has "transparency disabled" according to Photoshop.

Is the replicable outside my system on ImageMagick 6.9.1-10 Q16?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Inconsistent alpha flattening of transparent TIFs

Post by fmw42 »

It works fine for me on IM 6.9.1.10 Q16 Mac OSX.

If you want to flatten the alpha to white, then do

Code: Select all

convert bad.tif -profile /users/fred/images/profiles/sRGB.icc -background white -compose over -flatten bad_test1.tif
If you want to remove the alpha, then do

Code: Select all

convert bad.tif -alpha off -profile /users/fred/images/profiles/sRGB.icc -background white -compose over -flatten bad_test2.tif
quicksnap
Posts: 5
Joined: 2015-08-06T21:34:40-07:00
Authentication code: 1151

Re: Inconsistent alpha flattening of transparent TIFs

Post by quicksnap »

Well, the problem with this is that the alpha channel is not a "transparency" channel. If you open that TIFF in Photoshop, you will not see any transparency--only the normal image with an additional channel:

https://dl.dropboxusercontent.com/spa/4 ... 2xou4p.png

The problem is that I need a way to detect this non-transparent alpha channel, so that I can run a conversion across all TIFFs, some of which have a standard alpha channel which needs to be flattened and cannot be removed.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Inconsistent alpha flattening of transparent TIFs

Post by snibgo »

The problem is that I need a way to detect this non-transparent alpha channel ...
Quite possibly this can come from identify -verbose "tiff:alpha", of exiftool "IFD0:ExtraSamples".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Inconsistent alpha flattening of transparent TIFs

Post by fmw42 »

The problem is that I need a way to detect this non-transparent alpha channel, so that I can run a conversion across all TIFFs, some of which have a standard alpha channel which needs to be flattened and cannot be removed.
Please clarify you comment. Why do not you need to detect alpha?

If you flatten the image whether it has alpha or not, my first command will work. If there is no alpha, it does not matter.

If you never want to to see the alpha as white when flattening, then the second command will work whether there is alpha or not.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Inconsistent alpha flattening of transparent TIFs

Post by snibgo »

I think his question is, "When I have an alpha, how do I know if it represents transparency?"

I think the answer is to look at identify -verbose "tiff:alpha", or exiftool "IFD0:ExtraSamples".
snibgo's IM pages: im.snibgo.com
Post Reply