How to Extract Layers from a TIF File and convert to PNG with transparacy

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?".
sandraR
Posts: 19
Joined: 2019-01-22T02:37:30-07:00
Authentication code: 1152

How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by sandraR »

Hi, I am new to working with ImageMagick.
I am using the version 7.0.8-24 on CentOS6.9

I want to extrakt a layer from a TIF file and convert this to a PNG (with transparacy).

My TIF file contains 2 layers.
The bottom layer is with a red background color, and the second layer is an image of a shoe (with transparacy).
Both layers is visible in the file.

I want to extract just the second layer and convert it into a PNG file (and keep the original image size and transparency).

I found this guide on the web, that I have been using (since I want to keep the original image size):
http://undertheweathersoftware.com/how- ... nd-python/

My command is:
convert "/path/to/my/testfile.tif[0]" "/path/to/my/testfile.tif[2]" \( -clone 0 -alpha transparent \) -swap 0 +delete -coalesce -compose src-over -composite "/path/to/my/testfile.png"

This works like a charm on ImageMagick v6.9.9 om MacOSX 10.14 - but it won't work properly on v7.0.8-24 on CentOS6.9. :(

On v7.0.8-24, I get a conversion with only the second layer to a PNG with transparacy - but the colorinformation is lost (the original file is RGB). So the results is now a greyscale and the product image displays it as a black box instead.

Can I still use convert with 7.0.8-24 och is this some older commandline...?

Anyone got any ideas on how I can get it to work?
sandraR
Posts: 19
Joined: 2019-01-22T02:37:30-07:00
Authentication code: 1152

Re: How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by sandraR »

I found this info on the webb:
"ImageMagick – I initially started implementing this with version 6.9.3, but have since upgraded to 7.0.1. I was taken aback a bit, since by default, versions 7.0.+ have marked the convert command as legacy, and the solution in this blog post uses the convert command. If you are using this version, be sure to check the option to “Install legacy utilities (e.g. convert)” before installing it. Older versions do not have this problem."

I have not installed any legacy utilities. I wonder if this could be the problem for 7.0.8-24?
But where can I get hold of these "legacy utilities"...?

Or perhaps this has nothing to do with the problem... hmmm...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by snibgo »

What does "convert -version" say? Is it v7 or v6?

For v7, I suggest you use "magick", not "convert" or "magick convert".

You say "My TIF file contains 2 layers" but you use [0] and [2] which is valid only if the tiff contains 3 images.

What does this say:

Code: Select all

magick identify /path/to/my/testfile.tif
How many images does that list?

It might help if you upload your tiff somewhere and paste a link here.
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 convert to PNG with transparacy

Post by sandraR »

I tried to change the "convert" to "magick" but it sadly it made no difference, the result was the same.
I still get a black box for the image, that should display a shoe image. But it looks like it picked the correct layer from the file thou.

As I explained earlier the command worked in v6.9.9 as it was.
So I got to figure out why it does not work for v7....?


The reason why the command states a layer [0] and [2], is because of what I read from the webbarticle:
http://undertheweathersoftware.com/how- ... nd-python/
Is stated in that article:
"Layer 0 is not really one of the layers in the PSD, but it’s actually the flattened image of all layers in the PSD. Layer 2 is the second layer in the PSD, for this example."

I have tried to just change where I put [0] to [1] everywhere in the command, but it did not work properly either.
I thought that maybe there was a diffrency how layers is interpreted for TIF as it does for PSD.

If I use the "identify" command - it gives me 3 layers: [0], [1], [2] - so it picks the correct layer.

Here is the TIF file I am trying from:download it form here
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by snibgo »

Your command is:

Code: Select all

convert \
  "/path/to/my/testfile.tif[0]" \
  "/path/to/my/testfile.tif[2]" \
  \( -clone 0 -alpha transparent \) \
  -swap 0 +delete -coalesce \
  -compose src-over -composite \
  "/path/to/my/testfile.png"
I'm not sure what you want. You read two images, then replace the first with a transparent version of itself. Then you composite the second image over that transparent image. So no colour from the first image shows in the result.

The second input is nearly gray, so the output is also nearly gray, a shoe on a transparent background.

Tested with v6.9.9-50 and "magick" on v7.0.7-28.
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 convert to PNG with transparacy

Post by sandraR »

Yes, your result is exacly what I want.
I want to use only the second layer from the TIF-file to create a new file with in PNG.
And since the second layer is a shoe with transparency, the PNG should be the same.

But on 7.0.8-24 is just gives me a black box instead of the shoe, and also the colorspace that should be RGB is now a greyscale. :(


May I ask you to run a "magick -version" on your 7.0.7 - I am curious to what features it has?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by snibgo »

I get, for v6 and v7:

Code: Select all

f:\web\im>%IM%convert -version
Version: ImageMagick 6.9.9-50 Q16 x64 2018-06-02 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype gslib heic jng jp2 jpeg lcms lqr lzma openexr pangocairo png ps raw rsvg tiff webp xml zlib

f:\web\im>%IMG7%magick -version
Version: ImageMagick 7.0.7-28 Q16 x64 2018-03-25 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC HDRI Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype gslib heic jng jp2 jpeg lcms lqr openexr pangocairo png ps raw rsvg tiff webp xml zlib
You can simply convert the [2] image to PNG:

Code: Select all

magick testfile.tiff[2] out.png
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: How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by fmw42 »

There appears to be a bug in Imagemagick 7. I will report it.
sandraR
Posts: 19
Joined: 2019-01-22T02:37:30-07:00
Authentication code: 1152

Re: How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by sandraR »

I finally got it to work in 7.0.8-24 on CentOS6 - but I had to change the command a bit...

This gets me the result I am looking for:
convert "/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.png"

I wanted to use only the second layer from the TIF-file, keeping the original size of the image and create a new file with it in PNG format (keeping the transparency).

YAY! It worked.
User avatar
fmw42
Posts: 25562
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 convert to PNG with transparacy

Post by fmw42 »

In Imagemagick 7, use magick, not convert. Using convert will revert to using Imagemagick 6 behavior.

Either of these work and are a bit simpler.

Code: Select all

magick testfile.tif \( -clone 0 -alpha set -channel rgba -evaluate set 0 \) -delete 0,1 +swap -layers merge testfile.png

or

magick testfile.tif \( -clone 0 -alpha transparent \) -delete 0,1 +swap -layers merge testfile.png
sandraR
Posts: 19
Joined: 2019-01-22T02:37:30-07:00
Authentication code: 1152

Re: How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by sandraR »

Well, your code did not keep the transparency in the file thou. But results was the shoe on a white background in version 7.0.8-24.

But this works:
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.png"

(both convert and magick commandline worked for me).

I also tried this on a PSD fil, and it works also. (just a side note)
User avatar
fmw42
Posts: 25562
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 convert to PNG with transparacy

Post by fmw42 »

Yes, sorry, I had not added -background none to my command, though I am puzzled why it is actually needed, since the first image is made transparent.

Anyway these work for me

Code: Select all

magick testfile.tif \( -clone 0 -alpha set -evaluate set 0 \) -delete 0,1 +swap -background none -layers merge testfile.png

or

magick testfile.tif \( -clone 0 -alpha transparent \) -delete 0,1 +swap -background none -layers merge testfile.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by snibgo »

fmw42 wrote:Yes, sorry, I had not added -background none to my command, though I am puzzled why it is actually needed, since the first image is made transparent.
"-layers merge" composites the first image over the background, then composites the second image over that. The default background is white.
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: How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by fmw42 »

Thanks, snibgo, for the clarification. I thought since the first image was transparent, the layers merge would just composite the second over the transparent first.
sandraR
Posts: 19
Joined: 2019-01-22T02:37:30-07:00
Authentication code: 1152

Re: How to Extract Layers from a TIF File and convert to PNG with transparacy

Post by sandraR »

Continuing this ticket, because I want to add some more settings for managing the colorspace and adding a diffrent colorprofile.

On my code that worked:

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.png"
I also wanted to try to add a conversion for the color profile.
The original file has the profile AdobeRGB1998.
So I want to change the colorspace and profile to sRGB profile to the output.
Here is the TIF file I am trying with:download it form here

This is what I tried:

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 -density 72 -resize 50% -set colorspace sRGB -profile "/path/to/profiles/sRGB Profile.icc" "/path/to/my/testfile.png"
= But this does not work properly, because if I try to open the file in Photoshop it keeps giving me an alert that says "it has a file format that does not support embedded profiles", so this means that the profile did not get embedded.

Anyone got any ideas on what I need to do to get the sRGB icc profile to get correctly embedded to the output file in PNG?

I have tried to find some help on: http://www.imagemagick.org/Usage/formats/#profiles
I have also tried this:
+profile icm -profile "/path/to/profiles/AdobeRGB1998.icc" -profile "/path/to/profiles/sRGB Profile.icc"
and this:
+profile icc -profile "/path/to/profiles/AdobeRGB1998.icc" -profile "/path/to/profiles/sRGB Profile.icc"
But it still won't embedd the profile.
Post Reply