PSD to png - black background

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
lucd
Posts: 22
Joined: 2018-09-11T14:50:41-07:00
Authentication code: 1152

PSD to png - black background

Post by lucd »

Hello,

Thanks in advance for helping !
I have a problem with just a few psds and can't find the reason.. getting nuts

All my psd files have one single layer visible, with transparency,
after i convert them to png with transparency, some images have a black background.
If i convert them to a jpg, with white background, all good

The same code converts many other files without any problem
i tried to find differences between offending / ok psd files in photoshop or in the imagemafick object when debugging, no success

Only thing found after trial & error, is that setting image.Alpha(AlphaOption.Disassociate) on the "black" psd removes this black layer
while on correct psds, it totally removes transparency
I guess that the black psd have some color set in the alpha layer ?

other thing, i create a new psd, i copy the visible layer from the "black" psd into the new one,
and this new one will be converted perfectely.

here is a link to download the PSD
https://www.dropbox.com/sh/373t7au4jv99 ... RgNqa?dl=0

Thanks to help,

Luc

User avatar
dlemstra
Posts: 1625
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: PSD to png - black background

Post by dlemstra »

I don't understand the problem that you are trying to solve. When an image has an alpha channel (RGBA) it is possible that the pixels (RGB) are transparent are black or white or maybe even purple. Where is this causing you issues?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

lucd
Posts: 22
Joined: 2018-09-11T14:50:41-07:00
Authentication code: 1152

Re: PSD to png - black background

Post by lucd »

Here are the psd files in photoshop
both have one single layer, with transparency (you can find originals in my first post)
same color space and so on.

Image

I convert them as png 24 with

Code: Select all

image.BackgroundColor = new MagickColor(MagickColors.Transparent);
And the result is:

Image

I can't get rid of that black layer ...
(i set the irfanview window color in gray to clearly show the alpha)

Thanks

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

Re: PSD to png - black background

Post by fmw42 »

I have no issue in command line with either image. They both convert with transparent background. Use the flattened layer.

Code: Select all

convert psd_ok.psd[0] -colorspace sRGB psd_ok.png
and

Code: Select all

convert psd_black.psd[0] -colorspace sRGB psd_black.png
I am using IM 6.9.10.10 Q16 Mac OSX with libpng 1.6.37

lucd
Posts: 22
Joined: 2018-09-11T14:50:41-07:00
Authentication code: 1152

Re: PSD to png - black background

Post by lucd »

THanks,

but how does this translate to magicknet configuration / command ?
What do you mean by "use the flattened layer" ?

I am getting the image from stream this way

Code: Select all



MagickReadSettings settings = new MagickReadSettings
                {
                    FrameIndex = 0,
                    FrameCount = 1,
                    Density = new Density(300, 300, DensityUnit.PixelsPerInch)
                };
MagickImageCollection magickImageCollection = new MagickImageCollection(content, settings);
 IMagickImage image = magickImageCollection.Flatten(new MagickColor(MagickColors.Transparent));
 
  image.BackgroundColor = new MagickColor(MagickColors.Transparent);
  
  // then resize
 
Thanks,

lucd
Posts: 22
Joined: 2018-09-11T14:50:41-07:00
Authentication code: 1152

Re: PSD to png - black background

Post by lucd »

If i don't convert the colorspace , then the layer stays transparent
(but then colors are totally off, since the source image is in CMYK and i am saving to png)

image.TransformColorSpace(ColorProfile.USWebCoatedSWOP, ColorProfile.SRGB);

lucd
Posts: 22
Joined: 2018-09-11T14:50:41-07:00
Authentication code: 1152

Re: PSD to png - black background

Post by lucd »

if i remove the alpha channel, the black one goes away,
but i lose the transparency on the standard ok psd

image.Alpha(AlphaOption.Off);

Image

lucd
Posts: 22
Joined: 2018-09-11T14:50:41-07:00
Authentication code: 1152

Re: PSD to png - black background

Post by lucd »

Got a solution... but i'd like to understand what's happening ??

the "black" one has total colors = 191
while the ok has thousands of colors

this works.. but why ?

Code: Select all

if (image.TotalColors < 256)
 image.Alpha(AlphaOption.Disassociate);
 
txs

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

Re: PSD to png - black background

Post by snibgo »

@lucd: You say "I convert them as png 24" but you don't show your complete code.

png24 has no alpha channel. It has 8-bit channels for RGB only. Therefore the result will have no transparency.
snibgo's IM pages: im.snibgo.com

lucd
Posts: 22
Joined: 2018-09-11T14:50:41-07:00
Authentication code: 1152

Re: PSD to png - black background

Post by lucd »

@snibgo

i was using image.Format = MagickFormat.Png24;

png24 supports the transparency, the code did process thousands of images.. with transparency :)
so i guess it depends of the source image as well, go figure

here is one png24 in PS ..
Image

still, i did set it to image.Format = MagickFormat.Png32; and problem solved !!

thanks for pointing out that direction !!
Last edited by lucd on 2020-05-09T19:05:05-07:00, edited 1 time in total.

lucd
Posts: 22
Joined: 2018-09-11T14:50:41-07:00
Authentication code: 1152

Re: PSD to png - black background

Post by lucd »

I am left with one problem on a tiff file

this tiff has transparency in photoshop, but i can't get the transparency after conversion to png32
i see in debugging that HasAlpha = false

in photoshop:
Image

afer conversion
Image

I get it from a stream using

Code: Select all

 
 magickImageCollection = new MagickImageCollection(content, settings);
image = magickImageCollection.Flatten(new MagickColor(MagickColors.Transparent));
 
then i set the format, resize, and that's it
Any idea ?

(do not pay attention to colors a bit off, i was viewing it using a proof setup in PS)

Thanks a lot,

Luc

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

Re: PSD to png - black background

Post by fmw42 »

Post the original TIFF file so we can inspect it.
i was using image.Format = MagickFormat.Png24;
I think you want PNG32 for transparency. See https://imagemagick.org/Usage/formats/#png_formats

lucd
Posts: 22
Joined: 2018-09-11T14:50:41-07:00
Authentication code: 1152

Re: PSD to png - black background

Post by lucd »

Indeed better with a link to the file, sorry

https://www.dropbox.com/s/ph1j8c8lryzj7tl/tif1.tif?dl=0

i did set the format to png 32, solved all problems but this tiff

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

Re: PSD to png - black background

Post by snibgo »

That tif file contains two images, with different sizes. Only the second image has an alpha channel.

Please show your complete code. Perhaps you are reading both images, and flattening them together.
snibgo's IM pages: im.snibgo.com

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

Re: PSD to png - black background

Post by fmw42 »

The transparency is coming from a clip path. Here is how to handle that in command line. Sorry, I do not know Magick.Net

Code: Select all

convert -quiet tif1.tif[0] -alpha transparent -clip -alpha opaque -strip tif1.png

Locked