Issue with dealing TIFF files, contains channels and transparency

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?".
Carter J
Posts: 116
Joined: 2013-12-19T02:20:09-07:00
Authentication code: 6789

Issue with dealing TIFF files, contains channels and transparency

Post by Carter J »

While converting TIFF files to JPEG, we are using below command:

Case 1:
To deal with Tiff files which contains multiple channels, we have included "alpha off" in the command and worked fine

Code: Select all

convert -alpha off inputImage[0] -units pixelsperinch -resample 150 outputImage.jpg 
Sample file:
https://www.dropbox.com/s/7sxzqjjbuoz0m ... a.tif?dl=0

Case 2:
For some TIFF files, we are losing transparency and background becomes black, when we tested by removing "alpha off", the output was fine without black background

Code: Select all

convert inputImage[0] -units pixelsperinch -resample 150 outputImage.jpg
Sample file:
https://www.dropbox.com/s/1gh3xf99ohlco ... e.tif?dl=0

Now, we want use a generic command to deal with all tiff files, irrespective of channels and background transparency

If Generic command not possible, then how to detect files that contains multiple channels, so that we will include "alpha off"

We tried below commands, but got same result for above two files

Code: Select all

identify -format '%[channels]' inputImage.tif
Comparison of identify -verbose tiff files from two cases, we have noted files which contains multiple channels contains below entries:

Code: Select all

"Alpha: srgba(243,243,245,0)"
What does it indicates?

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

Re: Issue with dealing TIFF files, contains channels and transparency

Post by fmw42 »

JPEG does not support transparency. So you cannot maintain transparency in your output. It will get removed whether you use -alpha off or not. So I am confused what you are trying to do. What does work fine in the first case do? Can you provide both input and output results, so we know what the exact issue is?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Issue with dealing TIFF files, contains channels and transparency

Post by snibgo »

I don't know what results you want. Perhaps this will do it:

Code: Select all

convert logo-alpha.tif -background white -flatten a.jpg

Code: Select all

convert TransparentBGTiffFile.tif -background white -flatten a.jpg
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: Issue with dealing TIFF files, contains channels and transparency

Post by fmw42 »

Your first file has just a single alpha channel, which is fine for Imagemagick. Your second file has multiple layers including some alpha masking and the only layer that is active is the transparency layer. You also some alpha channels that are turned off. You have selected layer [0] which is the alpha masking layer and then turn off transparency. What you see is the black background under the alpha mask, which has mostly a black background and some gray areas. So the results you are getting for both images is correct, as far as I can tell.

In the first example, you seem to want to see what is under the transparent area.

In the second example, you do not want to see everything under the transparent area.

If you want to get rid of the gray areas that are in the background of the second example, then this will do that.

Code: Select all

convert TransparentBGTiffFile.tif -background black -alpha background -flatten -units pixelsperinch -resample 150 tmp2.jpg
The problem is that it will make the transparent areas of your first example black. So you should only do this if the image without transparency has other things that you want hidden by the alpha mask rather than just turning alpha off. Also you need to decide what background color you want to use.

So, bottom line. It is working fine with just -alpha off. The problem is that your underlying image may contain things you do not want shown. Then you have to decide which approach to use. I see no automated one command approach.
Carter J
Posts: 116
Joined: 2013-12-19T02:20:09-07:00
Authentication code: 6789

Re: Issue with dealing TIFF files, contains channels and transparency

Post by Carter J »

fmw42 wrote:Your first file has just a single alpha channel, which is fine for Imagemagick. Your second file has multiple layers including some alpha masking and the only layer that is active is the transparency layer. You also some alpha channels that are turned off. You have selected layer [0] which is the alpha masking layer and then turn off transparency. What you see is the black background under the alpha mask, which has mostly a black background and some gray areas. So the results you are getting for both images is correct, as far as I can tell.

In the first example, you seem to want to see what is under the transparent area.

In the second example, you do not want to see everything under the transparent area.

If you want to get rid of the gray areas that are in the background of the second example, then this will do that.

Code: Select all

convert TransparentBGTiffFile.tif -background black -alpha background -flatten -units pixelsperinch -resample 150 tmp2.jpg
The problem is that it will make the transparent areas of your first example black. So you should only do this if the image without transparency has other things that you want hidden by the alpha mask rather than just turning alpha off. Also you need to decide what background color you want to use.

So, bottom line. It is working fine with just -alpha off. The problem is that your underlying image may contain things you do not want shown. Then you have to decide which approach to use. I see no automated one command approach.


Thanks for looking into it..

To deal with below type of files, which contains multiple channels as suggested in link "viewtopic.php?f=1&t=22918", we have included "alpha off"

input file link:
https://www.dropbox.com/s/7t2qkk9yzaops ... e.tif?dl=0

output file if we don't use "alpha off":
https://www.dropbox.com/s/4xfkn016ey02k ... e.jpg?dl=0


Now, we are trying to use a generic command which works in all cases, if we use that some files background is black, so for that files we removed "alpha off" and then output is good


"The problem is that your underlying image may contain things you do not want shown. Then you have to decide which approach to use. I see no automated one command approach"


If it is impossible for one command approach, how to differentiate files with channels...So, that we will add "alpha off " only for those files

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

Re: Issue with dealing TIFF files, contains channels and transparency

Post by fmw42 »

The issue is not -alpha off. It is whether you have stuff in the background that you need to mask out. Both images can be processed the same way.

Code: Select all

convert inputImage[0] -alpha off -units pixelsperinch -resample 150 outputImage.jpg
The problem is that the first image has meaningful texture below the transparency. The second image is mostly black, but has some gray areas below the transparency. If you do not like the gray areas, then you must do the extra processing that I showed above.

If you always want to blank out the transparent areas, then use the

Code: Select all

convert  inputImage[0] -background desiredcolor -alpha background -flatten -units pixelsperinch -resample 150 outputImage.jpg
but you then must first decide what the background color should be converted to under the transparency.

You could check the color of the top left pixel as one approach and use that color.

unix syntax:

Code: Select all

color=`convert inputImage[0] -alpha off -format "%[pixel:u.p{0,0}]" info:`
convert inputImage[0] -background $color -alpha background -flatten -units pixelsperinch -resample 150 outputImage.jpg
Carter J
Posts: 116
Joined: 2013-12-19T02:20:09-07:00
Authentication code: 6789

Re: Issue with dealing TIFF files, contains channels and transparency

Post by Carter J »

Thanks for the detailed info..

In identify -verbose of some images:

Red:
min: 28 (0.109804)
max: 247 (0.968627)
mean: 219.575 (0.861078)
standard deviation: 60.2877 (0.236422)
.....
Green:
min: 32 (0.12549)
max: 243 (0.952941)
mean: 217.206 (0.851787)
.....
Blue:
min: 33 (0.129412)
max: 248 (0.972549)
mean: 220.999 (0.866664)
...
Alpha:
min: 0 (0)
max: 255 (1)
mean: 103.237 (0.404851)
....
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 202.386
....
Alpha: srgba(243,243,245,0) #F3F3F500

What Does Alpha: meant here?

How to get if a image has that value directly using a command other than -verbose (like for profiles we use identify -format ['%profiles'])

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

Re: Issue with dealing TIFF files, contains channels and transparency

Post by fmw42 »

I thik it comes only when you have an image that is type=palettealpha. That is then the alpha value used in pseudocolor table. But I do not think it is relevant to your above processing. There is no current string form (http://www.imagemagick.org/script/escape.php) to get that in a simple way. You would have to parse the verbose information to just display that information.

For example:
convert rose: -transparent white -type palettealpha -verbose info:
Channel statistics:
Pixels: 3220
Red:
min: 0 (0)
max: 255 (1)
mean: 143.991 (0.564669)
standard deviation: 69.6836 (0.273269)
kurtosis: -1.33359
skewness: 0.126826
entropy: 0.955509
Green:
min: 0 (0)
max: 254 (0.995132)
mean: 87.5716 (0.343418)
standard deviation: 51.2857 (0.20112)
kurtosis: 2.76018
skewness: 1.79292
entropy: 0.955322
Blue:
min: 0 (0)
max: 253 (0.991852)
mean: 78.7891 (0.308977)
standard deviation: 53.7794 (0.2109)
kurtosis: 3.17484
skewness: 1.98101
entropy: 0.95353
Alpha:
min: 0 (0)
max: 255 (1)
mean: 253.337 (0.993478)
standard deviation: 20.5258 (0.0804935)
kurtosis: 148.34
skewness: 12.2613
entropy: 0.00717468
Image statistics:
Overall:
min: 0 (0)
max: 255 (1)
mean: 78.0036 (0.305896)
standard deviation: 51.9605 (0.203767)
kurtosis: 8.75716
skewness: 2.65404
entropy: 0.717884
Alpha: none #00000000
Colors: 248

But if the -type is left off or set to truecoloralpha, then there will be no entry for Alpha
Carter J
Posts: 116
Joined: 2013-12-19T02:20:09-07:00
Authentication code: 6789

Re: Issue with dealing TIFF files, contains channels and transparency

Post by Carter J »

fmw42 wrote:I thik it comes only when you have an image that is type=palettealpha. That is then the alpha value used in pseudocolor table. But I do not think it is relevant to your above processing. There is no current string form (http://www.imagemagick.org/script/escape.php) to get that in a simple way. You would have to parse the verbose information to just display that information.

But if the -type is left off or set to truecoloralpha, then there will be no entry for Alpha
Thanks you for redirecting..

We have a automated process which invokes conversions upon receiving files
Now,
1.) How to detect image has transparent layer without opening using IM commands? (to include "alpha off" for all, except for those images, which contains transparency).
or
2.) How to detect below type of problematic files image without transparency has other things that you want hidden by the alpha mask rather than just turning alpha off

input file link:
https://www.dropbox.com/s/7t2qkk9yzaops ... e.tif?dl=0

output file if we don't use "alpha off":
https://www.dropbox.com/s/4xfkn016ey02k ... e.jpg?dl=0


My desired output should be all files (regardless of transparent layer and other areas under transparency) should have background (not black as explained below) and complete image (not like output file provided in above link)

Any suggestions will be of great help
Carter J
Posts: 116
Joined: 2013-12-19T02:20:09-07:00
Authentication code: 6789

Re: Issue with dealing TIFF files, contains channels and transparency

Post by Carter J »

Hi,

Could anyone provide any update on how to detect image has transparent layer?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Issue with dealing TIFF files, contains channels and transparency

Post by snibgo »

IM doesn't deal with layers.

Code: Select all

convert in.tiff -format %[opaque] info:
This returns "true" if all the pixels are fully opaque, or "false" if there is any transparency.

Does that answer your question?
snibgo's IM pages: im.snibgo.com
Carter J
Posts: 116
Joined: 2013-12-19T02:20:09-07:00
Authentication code: 6789

Re: Issue with dealing TIFF files, contains channels and transparency

Post by Carter J »

snibgo wrote:IM doesn't deal with layers.

Code: Select all

convert in.tiff -format %[opaque] info:
This returns "true" if all the pixels are fully opaque, or "false" if there is any transparency.

Does that answer your question?
Thanks for reply..

Actually we are trying to figure out what "alpha off" makes the difference in output for below two files:

I have added commands and output files for reference

1. inputFile: (output is fine when we removed "alpha off")
https://www.dropbox.com/s/1gh3xf99ohlco ... e.tif?dl=0

outputFile:
https://www.dropbox.com/s/utgtbqzyrbs20 ... t.jpg?dl=0

2.inputFile:
https://www.dropbox.com/s/7t2qkk9yzaops ... e.tif?dl=0

outputFile:
https://www.dropbox.com/s/4xfkn016ey02k ... e.jpg?dl=0

Command Used:

convert -alpha off inputFile.tif[0] -units pixelsperinch -resample 200 ouptputFile.jpg

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

Re: Issue with dealing TIFF files, contains channels and transparency

Post by snibgo »

According to "identify", MultiChannelTiffFile.tif has RGB and A but no clip path, so removing transparency is easy:

Code: Select all

convert -verbose MultiChannelTiffFile.tif -alpha off x.jpg
TransparentBGTiffFile.tif is more awkward. Transparent pixels contain data that is hidden by either the alpha transparency, or the clip path. You (probably) don't want to see this data. We can turn those pixels white:

Code: Select all

convert -verbose TransparentBGTiffFile.tif -background White -flatten y.jpg
I put "-alpha off" after the input filename, not before.
snibgo's IM pages: im.snibgo.com
Carter J
Posts: 116
Joined: 2013-12-19T02:20:09-07:00
Authentication code: 6789

Re: Issue with dealing TIFF files, contains channels and transparency

Post by Carter J »

snibgo wrote:According to "identify", MultiChannelTiffFile.tif has RGB and A but no clip path, so removing transparency is easy:

Code: Select all

convert -verbose MultiChannelTiffFile.tif -alpha off x.jpg
TransparentBGTiffFile.tif is more awkward. Transparent pixels contain data that is hidden by either the alpha transparency, or the clip path. You (probably) don't want to see this data. We can turn those pixels white:

Code: Select all

convert -verbose TransparentBGTiffFile.tif -background White -flatten y.jpg
I put "-alpha off" after the input filename, not before.
Thanks for Info..

Now, how to detect "Transparent pixels contain data that is hidden by either the alpha transparency, or the clip path." using IM commands.

We are trying for a Generic command to deal with both types of files..

If we include "alpha off", many files losing background (showing black background)
or
if we remove "alpha off", some files like MultiChannelTiffFile.tif, has weird output.

We just want to determine/divide files to inlcude/remove alpha off using verbose data? Any common data/command to divide them?

Any suggestions would be of great help?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Issue with dealing TIFF files, contains channels and transparency

Post by snibgo »

The same command works for both images:

Code: Select all

convert MultiChannelTiffFile.tif -backgound white -flatten x2.jpg
snibgo's IM pages: im.snibgo.com
Post Reply