export levels' psd

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
iacoposk8
Posts: 7
Joined: 2014-08-23T11:25:25-07:00
Authentication code: 6789

export levels' psd

Post by iacoposk8 »

I would like to create a script in php (or another language) that exports all layers of a psd file. I wrote:

Code: Select all

<?php
    $im = new Imagick("Senza titolo-1.psd");
    for ($i = 0, $num_layers = $im->getNumberImages(); $i < $num_layers; ++$i) {
        $im->writeImage('layer_' . $i . '.png');
    }


    $im->flattenImages();
    $im->setImageFormat('png');
    $im->writeImage('test.png');
?>
the first half of code, export layers, the second half layers merged. if we do the test with this file:
https://drive.google.com/file/d/0Bzaq0S ... sp=sharing

I see that exports 3 black arrows while the psd contains a single blue arrow. how do I fix it?

ps: I'm okay with any method, even without Imagick
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: export levels' psd

Post by dlemstra »

Why do you want to merge the layers yourself? The first image is the combined image. You can read it like this:

Code: Select all

$im = new Imagick("Senza titolo-1.psd[0]");
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
iacoposk8
Posts: 7
Joined: 2014-08-23T11:25:25-07:00
Authentication code: 6789

Re: export levels' psd

Post by iacoposk8 »

I tried, but the exported image is not only the blue arrow, but the arrow blue with a white background. I would like to have only the arrow cropped with transparent background.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: export levels' psd

Post by fmw42 »

You have no one layer that is what you want. You will need to combine layers with some code to get it. This should do it.

Unix syntax:

Code: Select all

convert Senza_titolo-1.psd[0] \
\( +clone -fill black -colorize 100 -alpha transparent \) \
\( Senza_titolo-1.psd[2] -negate -alpha extract \) \
\( -clone 1 -clone 2 -background none -flatten -alpha off \) \
-delete 1,2 \
-compose over -compose copy_opacity -composite result.png
For Windows, see syntax changes at http://www.imagemagick.org/Usage/windows/
iacoposk8
Posts: 7
Joined: 2014-08-23T11:25:25-07:00
Authentication code: 6789

Re: export levels' psd

Post by iacoposk8 »

Thank you very much! you have been very kind.
you can make it variable? I'll explain:
now the script to work it needs to indicate that the [0], [2] etc. .. you can put it all in a loop so that I export all levels and when it encounters a case like this be able to handle it.
and if we could also crop images, it would be perfect!
iacoposk8
Posts: 7
Joined: 2014-08-23T11:25:25-07:00
Authentication code: 6789

Re: export levels' psd

Post by iacoposk8 »

is there a way to know if the layer is of type "combine"?
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: export levels' psd

Post by dlemstra »

The first 'image/layer' is the combined image.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
iacoposk8
Posts: 7
Joined: 2014-08-23T11:25:25-07:00
Authentication code: 6789

Re: export levels' psd

Post by iacoposk8 »

but I can understand it via code?
so that if I export a layer in a foreach, if I find one of this type use the code fmw42
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: export levels' psd

Post by fmw42 »

there is no guarantee that another image will need the same combination of layers that I used in my code for your image. In fact, another image may need totally different processing of layers to achieve what you may want.

I had to write out each layer of your image and examine then separately to see what code was needed to achieve what you wanted.
iacoposk8
Posts: 7
Joined: 2014-08-23T11:25:25-07:00
Authentication code: 6789

Re: export levels' psd

Post by iacoposk8 »

and to export the combined layer there is another way?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: export levels' psd

Post by fmw42 »

For your combined layer, no. But the first layer of the PSD file should be the combination of all layers flatted. But in your case, that was not what you wanted. So one has to examine each layer and decide how to combine them, if the first layer (image.psd[0]) is not what you want. Or create a new PSD file in Photoshop that does what you want for the combination of all the layers (as the combined layer [0]).
iacoposk8
Posts: 7
Joined: 2014-08-23T11:25:25-07:00
Authentication code: 6789

Re: export levels' psd

Post by iacoposk8 »

OK. thank you for your response.
I thought it was easier, because I found so many scripts to photoshop online, even amateur, with a few lines where you could do.
a common thing that I saw in all the script is this:
are hidden all levels, then one by one were displayed and exported.
even doing so you could solve my problem?
thank you :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: export levels' psd

Post by fmw42 »

If all the PDS files have the very same combination of layers, then the code I sent would do what you want. But if the PSD files have other combination of layers, then that same code would not work.
Post Reply