Problem with PSD to PNG conversion (Opacity and Transparent Background)

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
Rahul
Posts: 8
Joined: 2016-09-12T15:52:59-07:00
Authentication code: 1151

Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by Rahul »

Greetings,

When I use ImageMagick command-line tool to convert PSD to PNG. I have noticed a behavior which is strange and hard for me to understand. I will try my best to put factual information here.

What's in the PSD file?
I'm using Photoshop CC to construct PSD file. I have a transparent background and I have one visible shadow layer with R=0 G=0 B=0 and varying opacity values around shadow region. At the borders opacity values are 0% and as you move from borders to center region opacity value increase to ~30%.

ImageMagick command-line to convert to PNG:
(interested in Composite layer hence appended [0])
> convert.exe input.psd[0] output.png

Observations:
- The output PNG file doesn't retain the same RGB values. Though it retains Opacity values as input PSD file. For example, in PSD input file, if I have R=G=B=0 opacity= 29% at pixel [100x100] then output PNG file has R=195 G=195 B=195 Opacity=29%. These are not random numbers to me it represent remaining 71% of opacity value applied to the color channels. All pixels around shadow region has the same logic.

- I have used PhotoshopCC > SaveAs option and GIMP > Export feature to convert PSD to PNG file and it works perfectly fine - RGB values matches.

- When I compare PNGs saved by PhotoshopCC/GIMP vs ImageMagick output PNG. Shadow color changed from Black foreground to White foreground. So, if you have White background applied to PNG then you will notice that shadow is lighter and smaller. Otherwise with non-white background you will see White foreground in shadow region (same size).

- In that case I decided to debug my PSD reader code (no library used) and it looks like we were reading the same RGB values that we pass to generate PNG file.

Questions:
1. What is actually stored in PSD file? How to cross-examine pixel data?
2. Is it broken? How can I fix it?
(As a hack- I managed to convert all colorspace value back to 0 and it resembles the output produced by PhotoshopCC.)

I took a screenshot of input PSD file and output produced by PhotoshopCC and ImageMagick - https://drive.google.com/file/d/0B0eLSA ... sp=sharing

Let me know if you need more information in order to help me. I really appreciate your time!

Thanks & Regards,
Rahul Patel
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by snibgo »

I know nothing about PSD files.

If you link to the actual PSD file, someone might take a look.
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: Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by fmw42 »

What is your IM version and platform? Please always provide that information.

See http://www.imagemagick.org/script/comma ... php#define where there is a define that can be used.

"psd:alpha-unblend=off Disables new automatic un-blending of transparency with the base image for the flattened layer 0 before adding the alpha channel to the output image. This define must be placed before the input psd image. (Available as of IM 6.9.2.5). The automatic un-blending is new to IM 6.9.2.5 and prevents the transparency from being applied twice in the output image."

Does that help?

As user snibgo said, please provide an example PSD file. You can upload to some free hosting service such as dropbox.com and put the URL here.
Rahul
Posts: 8
Joined: 2016-09-12T15:52:59-07:00
Authentication code: 1151

Re: Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by Rahul »

Thanks for your quick replies!

I'm using ImageMagick v6.5.8-Q8 on Windows Server 2012 R2

Original PSD file:
https://dl.dropboxusercontent.com/u/799 ... 6_2023.psd

Output PNG file:
https://dl.dropboxusercontent.com/u/799 ... v6.5.8.png

In the meantime, I will check and test this recommendation "psd:alpha-unblend=off" suggested by fmw42 user.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by fmw42 »

My PS CS (very old v8) won't even open it. So you must be using some newer features. Do you have layers other than image layers, such as group layer or adjustment layers. If so, then IM likely will not handle them correctly.

Can you make a screen snap of the layers window and provide the link to it?

I also get an abort trap on IM 6.9.5-9 Q16 on my Mac OSX with

Code: Select all

identify -verbose Final_Only_Shadow_JO2016_2023.psd
Abort trap
Perhaps the uploaded file is corrupt? Can you zip it and upload again? Perhaps the zipping will protect it from corruption.
Rahul
Posts: 8
Joined: 2016-09-12T15:52:59-07:00
Authentication code: 1151

Re: Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by Rahul »

Hi fmw42,

My Original art work does have couple of groups with two layers inside. But, here's the thing - I have removed all the layer and kept one layer which has shadow in it and it still generate wrong result.

I'm using Photoshop CC 2015

Layer Dialog for the PSD file:
https://dl.dropboxusercontent.com/u/799 ... s_info.png

I was able to run this command on uploaded image.
Command - "identify -verbose Final_Only_Shadow_JO2016_2023.psd"
https://dl.dropboxusercontent.com/u/799 ... output.txt

I have zipped PSD file:
https://dl.dropboxusercontent.com/u/799 ... 6_2023.zip

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

Re: Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by fmw42 »

I do not understand this, but I still get an abort trap trying to run identify -verbose and PS CS hangs when trying to open your image after unzipping. identify (without the -verbose) on the first (flattened) layer works, but it fails if adding -verbose.

IM 6.9.5-9 Q16 on my Mac OSX

If you can run it, then try the define.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by snibgo »

Rahul wrote:I'm using ImageMagick v6.5.8 ...
That is old. Very old. I suggest you upgrade.

With a more recent version, v6.9.5-3, converting the file in your zip:

Code: Select all

convert Final_Only_Shadow_JO2016_2023.psd[0] x.png
x.png has 16295 pixels that are fully-transparent white. The others are black with various transparencies. Is that what you are expecting?

(Unlike Fred, I had no problems with "identify -verbose", which shows two images.)
snibgo's IM pages: im.snibgo.com
Rahul
Posts: 8
Joined: 2016-09-12T15:52:59-07:00
Authentication code: 1151

Re: Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by Rahul »

Hello again,
Sorry it took me sometime to get back to you guys.

I have downloaded ImageMagick-6.9.5-9-Q18-x86 version and it generate correct result for composite layer.
It would be nice to know what was the problem and how it got fixed in ImageMagick. I will search around but if any of you have any pointers then please share with me.

One more thing - I would like to dump all the pixel data similar to "identity -verbose" command to see how these pixels are stored in PSD or for that matter any image formats.

Snigbo - x.png has 16295 pixels that are fully-transparent white. The others are black with various transparencies. Is that what you are expecting? Yes
(When I try to read pixels data from PSD file I received blended transparency colors, as mentioned in first post, for various transparencies. I thought that was original problem. And, after some testing it seems later version of ImageMagick invert this operation before reading pixel values. Just my guess and need confirmation.)

Thank you snibgo and fmw42 for all your support!
Rahul
Posts: 8
Joined: 2016-09-12T15:52:59-07:00
Authentication code: 1151

Re: Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by Rahul »

To answer my previous question:
File: psd.c
Where I found it: https://github.com/ImageMagick/ImageMag ... ders/psd.c

/*
For some reason Photoshop seems to blend semi-transparent pixels with white.
This method reverts the blending. This can be disabled by setting the
option 'psd:alpha-unblend' to off.
*/
Function: static MagickBooleanType CorrectPSDAlphaBlend(const ImageInfo *image_info, Image *image,ExceptionInfo* exception)

I think this comment exactly summarized my problem and how IM has corrected it. Thank you IM!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem with PSD to PNG conversion (Opacity and Transparent Background)

Post by fmw42 »

That was the define I mentioned in one of my earlier posts.
Post Reply