List of TIFFs to layers in PSD, then back to TIFFs

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
Sulphur
Posts: 5
Joined: 2015-06-02T07:37:50-07:00
Authentication code: 6789

List of TIFFs to layers in PSD, then back to TIFFs

Post by Sulphur »

I'd like to be able to automatically (no input from user) do the following:
  • take a bunch of TIFFs (they are all the same size/colour range/etc.)
  • create a PSD where each TIFF is a layer
  • name the layers to something user-friendly
THEN, go the other way:
  • take a PSD with a bunch of layers
  • export each layer to a TIFF, naming the file a specific name (preferably overwriting the input TIFFs)
Imagine we have:
  • 20150602_classification.tiff
  • 20150602_height.tiff
  • 20150602_intensity.tiff
I want this to create a PSD with layers:
  • classification
  • height
  • intensity
I can achieve this manually by using PhotoShop:
File -> Scripts -> Load Files into Stack...

And then the other way:
File -> Scripts -> Export Layers to Files...

But this is manual, I want automation, am I correct in thinking this can be achieved? (possibly not the layer and file naming?)

I thought I got half-way there with convert and adjoin, but realised it wasn't quite right...
I tried:

Code: Select all

convert -adjoin 20150602_classification.tiff -adjoin 20150602_height.tiff -adjoin 20150602_intensity.tiff work.psd
But this did not give me what I wanted, so I misunderstand adjoin...

Thanks for any help
Sulphur
Posts: 5
Joined: 2015-06-02T07:37:50-07:00
Authentication code: 6789

Re: List of TIFFs to layers in PSD, then back to TIFFs

Post by Sulphur »

Ok, searching around, I've got this so far...

Code: Select all

convert ( -label "Intensity" 20150602_intensity.tiff -background none -flatten ) ( -label "Classification" 20150602_classification.tiff -background none -flatten ) ( -label "Height"  20150602_height.tiff -background none -flatten ) ( -clone 0--1 -flatten ) -reverse batch_work.psd
(Changing flatten for mosaic doesn't seem to do anything?)
(And I'm confused about the need for clone and reverse...?)

But, for some reason the 20150602_intensity.tiff, which is RGB, but greyscale values, comes out blue...?

Any help with that?
Or splitting up back to TIFFs?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: List of TIFFs to layers in PSD, then back to TIFFs

Post by fmw42 »

Depending upon the order you want the images, try

Code: Select all

convert -respect-parentheses ^
( -label "Intensity" 20150602_intensity.tiff ) ^
( -label "Classification" 20150602_classification.tiff ) ^
( -label "Height"  20150602_height.tiff ) ^
\( -clone 0-2 -background none -flatten) ^
-reverse batch_work.psd
The clones and flatten at the end make the flattened layer that PSD needs as the first layer. So I have reversed the order of all the layers

The parentheses restricts the labeling to that given image.

Alternately

Code: Select all

convert -respect-parentheses ^
( -label "Intensity" 20150602_intensity.tiff ) ^
( -label "Classification" 20150602_classification.tiff ) ^
( -label "Height"  20150602_height.tiff ) ^
\( -clone 0-2 -background none -flatten) ^
-insert 0 batch_work.psd
Here the -insert 0, moves the last image (the fattened layer) to the be the first layer without reversing all the other layers.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: List of TIFFs to layers in PSD, then back to TIFFs

Post by fmw42 »

PS If the images are not the same size, then you may need to use -mosaic ( or -layers merge) rather than -flatten, so that the size will be that of the largest image. But then you will need to control the page offset of each image to align it the way you might want.

see
http://www.imagemagick.org/Usage/layers/#mosaic
http://www.imagemagick.org/Usage/layers/#merge
Sulphur
Posts: 5
Joined: 2015-06-02T07:37:50-07:00
Authentication code: 6789

Re: List of TIFFs to layers in PSD, then back to TIFFs

Post by Sulphur »

Thanks fmw42... I think I understand a bit better now.

I now have this for creating the PSD:

Code: Select all

convert -respect-parentheses ^
 ( -label "Height"         20150602_height.tiff ) ^
 ( -label "Intensity"      20150602_intensity.tiff ) ^
 ( -label "Classification" 20150602_classification.tiff ) ^
 ( -clone 0-2 -background none -flatten ) ^
 -insert 0 ^
 batch_work.psd
(If I have more than 3 files, I understand the "-clone 0-2" will have to change to "-clone 0--1" or "-clone 0-x" where x is count of [images - 1])

But I still have an issue with this creating a blue layer for "Intensity" (but only when opened in PhotoShop) - see Issue 1 below.

To go back to TIFFs (from PSD), I have this:

Code: Select all

convert batch_work.psd[0] classification.tiff
convert batch_work.psd[1] height.tiff
convert batch_work.psd[2] intensity.tiff
(But this doesn't work so well if a layer is hidden in PhotoShop - see Issue 2 below)

So, I have two remaining issues, and a couple questions.

Issue 1
RGB image (intensity) but grey-scale values (10, 10, 10) (200, 200, 200) etc.
When opened in PhotoShop is shades of blue?
Note that if I change the order so that intensity is last (in the code above, i.e. the top layer), the other 2 images appear as grey-scale.
However, if I do the first script, then, without opening/saving the PSD file, run the second script, it all comes out as expected.
Suggestions?

Issue 2
Hidden layers in PSD not exported.
convert doesn't crash, but gives the nearest visible layer, I guess...
Is there a way to enforce that all layers are visible during this stage? Or perhaps ignore the visible state?

Questions
Where is the documentation for this method of extracting layers (or images) for a PSD (or multi-image image format)?

Code: Select all

convert in.psd[index] out.image_extension // documentation?
Can I query by layer name instead of index? (Perhaps the documentation would answer this...)

Code: Select all

convert in.psd[layer_name] out.image_extension
Thanks for any help!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: List of TIFFs to layers in PSD, then back to TIFFs

Post by snibgo »

What version IM are you using? I seem to remember an old bug, where color types were taken from the first in the list.
snibgo's IM pages: im.snibgo.com
Sulphur
Posts: 5
Joined: 2015-06-02T07:37:50-07:00
Authentication code: 6789

Re: List of TIFFs to layers in PSD, then back to TIFFs

Post by Sulphur »

Still have the problem with hidden layers, but...

I was using this:

Code: Select all

Version: ImageMagick 6.8.6-2 2013-06-23 Q8 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2013 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: ps
Which is shipped with Adobe Photoshop CC 2014.

But I tried running with this (latest release?):

Code: Select all

Version: ImageMagick 6.9.1-4 Q16 x64 2015-05-31 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features:  Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo freetype jng jp2 jpeg lcms lqr openexr pangoca
iro png ps rsvg tiff webp xml zlib
And it works! No more blue issue! Thanks snibgo!

However, the hidden layer issue remains (Issue 2 above)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: List of TIFFs to layers in PSD, then back to TIFFs

Post by snibgo »

Sulphur wrote:Can I query by layer name instead of index? (Perhaps the documentation would answer this...)
Sadly, you can't. The index has to be a zero-relative integer. I wanted something similar for Gimp. My solution was to write a Gimp script that creates a list of layer names.

IM can extract all layers from Gimp, whether hidden or not. I don't use Photoshop.
snibgo's IM pages: im.snibgo.com
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: List of TIFFs to layers in PSD, then back to TIFFs

Post by dlemstra »

IM will read all the layers, even the hidden ones. The compose property of the hidden layers will be set to 'no' for the invisible ones.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Sulphur
Posts: 5
Joined: 2015-06-02T07:37:50-07:00
Authentication code: 6789

Re: List of TIFFs to layers in PSD, then back to TIFFs

Post by Sulphur »

OK, yep, I can see that hidden layer are exported. But I still have a problem (with the top layer), which I hope to define more clearly...

First, let me describe what is happening with the generation of the PSD:
Using the command as above to create "batch_work.psd"... which lists the input files in this order:
  • Height
  • Intensity
  • Classification
I get a PSD file that has the following layers in the following order (top-bottom, as you'd see it)
  • Classification
  • Intensity
  • Height
So now I run my second script that does this:
  • convert batch_work.psd[0] classification.tiff
  • convert batch_work.psd[1] height.tiff
  • convert batch_work.psd[2] intensity.tiff
This outputs the correct layers to the correct files - which I find odd... (as intensity and height seem swapped)

Now, back to the PSD.
If I hide either or both Intensity/Height, and run the batch - I get the same output as if all layers are visible. Great, fantastic!
BUT, if I hide the top layer, Classification, and run the batch, the file, classification.tiff, comes out as the next visible layer below.
So if Intensity is visible, that's the data that goes into classification.tiff. (e.g. classification.tiff == intensity.tiff)
If Intensity is not visible, but Height is, the height's image data is what goes into classification.tiff. (e.g. classification.tiff == height.tiff)
If NO layer is visible, classification.tiff is filled with black.

Is there a way to remedy this?
Perhaps copy the PSD while setting the "compose" property to "yes" for all layers? How would I do that?
Post Reply