How to Extract Layers from a TIF File and save to new file with 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
sandraR
Posts: 19
Joined: 2019-01-22T02:37:30-07:00
Authentication code: 1152

How to Extract Layers from a TIF File and save to new file with transparency

Post by sandraR »

Hi.
I am using the IM version 7.0.8-24 on CentOS6.9

I wanted to extrakt a photoshop layer from a TIF file (with transparacy).

My TIF file contains 2 layers (if you look at it in photoshop).
The bottom layer is with a red background color, and the second layer is an image of a shoe (with transparency).
Both layers is visible in the file.

I want to extract just the second layer (and keep the original image size and transparency).

This gets me the result I am looking for:

Code: Select all

magick "/path/to/my/testfile.tif" \( -clone 0 -alpha transparent -channel rgba -evaluate set 0 \) -delete 0,1 -background none -compose src-over -layers merge "/path/to/my/testfile.tif"

BUT, here is the problem:

This new tif file I have created, if I open it in photoshopCC it looks just fine.

But If I try to use the file for layout purposes and place it into a InDesign CC document, the file displays with a black background where It should display transparency.
If I open the TIF file and resave it in photoshop, then it works just fine again.

Hmmm.... So my imagemagick code is not flawless for keeping the TIF format. It's like some technical info is missing from the file when using imagemagick since I can't display the transparency from the file in an Adobe program.

(I actually made this code before, but there I tried to convert the output to an PNG = that works just fine using the IM code and I can use the PNG file for layout purposes just fine!)

Anyone got any ideas how to improve this coding so IndesignCC displays the transparency from my (layered) TIF file?
I tried to layout it with Word, Powerpoint, but there it looks correct thou.

Here is the TIF file I am testing from:download it form here

sandraR
Posts: 19
Joined: 2019-01-22T02:37:30-07:00
Authentication code: 1152

Re: How to Extract Layers from a TIF File and save to new file with transparency

Post by sandraR »

I think I solved it, I had to define the output format with:

-define tiff:alpha=associated

Code: Select all

magick "/path/to/my/testfile.tif" \( -clone 0 -alpha transparent -channel rgba -evaluate set 0 \) -delete 0,1 -background none -compose src-over -layers merge -define tiff:alpha=associated "/path/to/my/testfile.tif"
This worked! Now adobe notice the transparency in the TIF file correct.

sandraR
Posts: 19
Joined: 2019-01-22T02:37:30-07:00
Authentication code: 1152

Re: How to Extract Layers from a TIF File and save to new file with transparency

Post by sandraR »

There is one problem I have noticed with the output file, after adding the -define tiff:alpha=associated.

The image in the file is a shoe, and there is a soft black shadow under it on the transparent background.
When I open the file in Photoshop I notice that the shadow is not exacly the same as in the original file.
The shadow appears much lighter, it is not as black, it looks like the shadow has lost some pixels in the image so it looks like it is disappearing.

This only happens if I use the -define tiff:alpha=associated in the commandline for the TIF output.
If I leave it out the coding the image looks ok, - but then I get my original problem back with the layout in indesign showing black where it suppose to be transparency.

If I try to convert the image to format PNG, then it looks ok for the shadow.
So this problem is with format TIF with transparency.

Anyone got any ideas?

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

Re: How to Extract Layers from a TIF File and save to new file with transparency

Post by snibgo »

I'm not sure what result you want. From your input tiff, the [2] image seems most useful.

Code: Select all

magick testshoe.tiff[2] +repage out.tiff
out.tiff has an opaque shoe. Shadow and background pixels are black, with varying transparency. The shadow varies in transparency from fully opaque to fully trnsparent.
snibgo's IM pages: im.snibgo.com

sandraR
Posts: 19
Joined: 2019-01-22T02:37:30-07:00
Authentication code: 1152

Re: How to Extract Layers from a TIF File and save to new file with transparency

Post by sandraR »

Snibgo, your example is not what I want to do. That example just gets me a new image with 2 layers.
See first post for more info on what I want and also what problem I got.
I solved my first problem in my second post in this thread.
I have gotten the result I want from the code I wrote:

Code: Select all

magick "/path/to/my/testfile.tif" \( -clone 0 -alpha transparent -channel rgba -evaluate set 0 \) -delete 0,1 -background none -compose src-over -layers merge -define tiff:alpha=associated "/path/to/my/testfile.tif"
But My new problem is that the output file shows the image differently when I look at it in photoshop.

Image

The above image you can see that the shadow is much darker, and in the bottom image is the output from IM, where the shadow is not the same.
IM does something to the image output when I add the -define tiff:alpha=associated to the commandline.
And if I takout the code the shadow looks ok, but then the layout problem in Indesign is the problem, so I need the code for Adobe to reqognize the transparancy when using file for layout.

So I am at a loss here...

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

Re: How to Extract Layers from a TIF File and save to new file with transparency

Post by fmw42 »

Have you tried

-define tiff:alpha=unassociated or unspecified

246246
Posts: 200
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: How to Extract Layers from a TIF File and save to new file with transparency

Post by 246246 »

sandraR wrote:
2019-02-25T06:09:55-07:00
Snibgo, your example is not what I want to do. That example just gets me a new image with 2 layers.
Here in 7.0.8-14, snibgo's command

Code: Select all

magick testshoe.tiff[2] +repage out.tiff
produce 1 layer tiff, as far as verified in magick identify.

There might be something in properties that is specific to Adobe, so, how about

Code: Select all

magick testshoe.tiff[2] +repage -strip out2.tiff

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

Re: How to Extract Layers from a TIF File and save to new file with transparency

Post by snibgo »

The input tiff does contain metadata that is specific to Photoshop, which might have some effect. However, it also contains an Adobe RGB (1998) ICC profile, and a simple "-strip" will remove it, which will change colours (because pixels will be assumed encoded as sRGB).

We can save the profile to a file, strip, then assign the profile:

Code: Select all

magick testshoe.tiff[2] ( +clone +write x.icc ) -strip x.icc out3.tiff
snibgo's IM pages: im.snibgo.com

Locked