Resizing PNG loses transparency

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
User avatar
MrChilBoy
Posts: 3
Joined: 2017-03-09T23:14:53-07:00
Authentication code: 1151

Resizing PNG loses transparency

Post by MrChilBoy »

I'm not sure what I'm doing wrong here but when I resize using the following command I lose the transparency of my PNG file.

This works fine (step one)

Code: Select all

magick convert logosrc.png -trim sample.png
...but this creates a white background (step 2).

Code: Select all

magick convert logosrc.png -trim -resize 600x600 sample.png
When I look at the identify -verbose of the original file and the file produced by step one, the following identifies the format (I think).

Code: Select all

Type: PaletteAlpha
Base type: TrueColorAlpha
It changes to the following after step 2 (and co-incidentally for another image I'm having issues with)

Code: Select all

Type: TrueColorAlpha
Using ImageMagick 7.0.5-1 Q16 x64 on Windows 10

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

Re: Resizing PNG loses transparency

Post by fmw42 »

After -trim add +repage. Does that help? If not, upload an example image to some free hosting service such as dropbox.com and put the URL here.

Also you are running the IM 6 legacy code by putting in convert. Remove the convert and just use magick.
User avatar
MrChilBoy
Posts: 3
Joined: 2017-03-09T23:14:53-07:00
Authentication code: 1151

Re: Resizing PNG loses transparency

Post by MrChilBoy »

Ok, turns out to be a weird bug in Picasa Photo Viewer ...sort of.

Code: Select all

magick logosrc.png sample.png
The results :
Original file -> Image
Resulting file -> Image

They look the same to some viewers but the identify -verbose outputs are different.

If I use the following code though, the identify -verbose ends up almost identical.

Code: Select all

magick logosrc.png -strip sample.png
Anyway, thanks for the tip about just using magick :)
User avatar
MrChilBoy
Posts: 3
Joined: 2017-03-09T23:14:53-07:00
Authentication code: 1151

Re: Resizing PNG loses transparency

Post by MrChilBoy »

Not sure why the links aren't working to the images :(

Original File -> https://dl.dropboxusercontent.com/u/992 ... ogosrc.png Image
Resulting File -> https://dl.dropboxusercontent.com/u/99232980/sample.png Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing PNG loses transparency

Post by fmw42 »

The two images are identical in pixel data.

Code: Select all

compare -metric rmse logosrc.png sample.png null:
0 (0)

Code: Select all

identify logosrc.png sample.png
logosrc.png PNG 197x201 197x201+0+0 8-bit sRGB 16.3KB 0.000u 0:00.000
sample.png PNG 197x201 197x201+0+0 8-bit sRGB 14.7KB 0.000u 0:00.000
The IM converted file may be slightly smaller size, because its zip compression may more efficient than the result from whatever tool you used

The IM converted file has three extra PNG meta tags:

Code: Select all

 png:bKGD: chunk was found (see Background color, above)
    png:cHRM: chunk was found (see Chromaticity, above)
    png:gAMA: gamma=0.45454544 (See Gamma, above)
So perhaps a different version of libpng was used. What tool created your original PNG?

Try adding an sRGB profile to each and see if they view the same. When I do:

Code: Select all

convert logosrc.png -profile /Users/fred/images/profiles/sRGB.icc logosrc_prof.png
convert sample.png -profile /Users/fred/images/profiles/sRGB.icc sample_prof.png
then they view the same.
Post Reply