Tiff conversion to PNG lost 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?".
Locked
nyro
Posts: 2
Joined: 2020-03-03T06:10:25-07:00
Authentication code: 1152

Tiff conversion to PNG lost transparency

Post by nyro »

This might be the same problem mentioned here, but not 100% sure...

I'm trying to convert TIFF files to PNG, by keeping transparency and avoiding many caveats coming from strange TIFF files.

These Tiff files can come from various sources (uploaded by users),
and the conversion should work as if we did it by simply opened the file in a image Editor and convert it into PNG.

I guess these files were created in PhotoShop.

I figured out a lot of problem for now, the only remaining problem is about transparency on some of them.

Here is a source file that I can share.

You'll see :
- it contains only 1 layer, which is a vector object
- vector object has a lot of layers, including a mask at the beginning
- the vector object is a rounded square
- space around this rounded square is transparent

When using :

Code: Select all

convert source.tif dest.png
it creates 2 PNG dest-0.png et dest-1.png
0 is the correct size, but with a white background
1 has the transparent, but the size not correct

First question: Why ImageMagick identify 2 layers?

Then, when using

Code: Select all

convert -define 'tiff:ignore-layers=true' -define 'tiff:alpha=unassociated' source.tif -background none -flatten +repage -strip dest.png
The file has a white background.
The define tags are here in order to fix other problem I encounter. Removing them have no influence on the result.

I can use the replace color technique, as there might have white pixel inside the image that shouldn't be transparent.

Main Question: How can I correctly convert this tif file into a transparent PNG?

I googled for hours about this problem, tested a lot of different combination and can't find a valid solution.

I'm using ImageMagick 6.9.7-4 (Debian)
I've also tested it with ImageMagick 7.0.8-40 through docker and the problem is exactly the same.

Thanks in advance.

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

Re: Tiff conversion to PNG lost transparency

Post by fmw42 »

try

Code: Select all

convert source.tif[1] result.png

nyro
Posts: 2
Joined: 2020-03-03T06:10:25-07:00
Authentication code: 1152

Re: Tiff conversion to PNG lost transparency

Post by nyro »

fmw42 wrote:
2020-03-03T10:45:20-07:00
try

Code: Select all

convert source.tif[1] result.png
Thanks @fmw42.
In this specific case, it will work, but the result image won't be in the correct size.

Moreover when the files will have multiple layers, the result won't be correct too.

I read that the first layer may be something added by Photoshop to make the preview easier.
Is there a way to identify such layer?

If so, I may be able to check if the tiff is in this case and use what you said, using something to resize the image correctly.

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

Re: Tiff conversion to PNG lost transparency

Post by fmw42 »

So it seems that the size comes from the first "layer" and the transparency from the second. So this works for this image.

IM 6

Code: Select all

dims=`convert source.tif[0] -format "%wx%h" info:`
convert source.tif[1] -gravity center -background none -extent $dims result.png
IM 7

Code: Select all

magick  source.tif[0] -set option:dims "%wx%h" source.tif[1] -gravity center -background none -extent $dims -delete 0 result2.png
If you have examples that do not work or need other processing, post a link to one of them.

Unfortunately, ImageMagick is not that advanced in reading Adobe (TIFF and PSD) files.

Locked