Page 1 of 1

Converting transparent PNG to Jpeg

Posted: 2007-02-01T09:25:26-07:00
by cs_rol
Hi! I'm using ImageMagick 6.3.2 with the MagickWand-API and I had troubles to convert and resize a partially transparent png to an jpg-file.
I tried the command line tools and the same things happend.

convert png.png png.jpg
--> a jpg-file is created and the transparent area is white

convert png.png -resize 500x400 png_small.jpg
--> a jpg-file in the right dimension is generated and the transparent area is black

What settings do i have to set if i want transparent areas always to be white?

thanks in advance
richard

PS: you can find the files:

http://www.werk3at.net/rol/png.png
http://www.werk3at.net/rol/png.jpg
http://www.werk3at.net/rol/png_small.jpg

Posted: 2007-02-01T10:53:55-07:00
by el_supremo
The original png has a white background colour but its transparent colour is "none". However, forcing the transparent background to white doesn't help. So, until the magicians give you the definitive fix, I have a workaround.

Code: Select all

convert png.png -resize 500x400 -size 500x400 xc:white +swap -compose over -composite png_small.jpg
This creates a white canvas and overlays the png image on top so that it has the background you want.

Pete

Posted: 2007-02-01T22:12:47-07:00
by anthony
First you will loss all transparent in JPG. It just don't understand it. As such whatever the transparent color in the image is, is what it uses. Your original used fully-transparent white (typical of PNG). However resize knows transparent is invisible, and as it is easier mathematically, it just makes all transparent colors, 'none' or fully transparent black.

It is just the way things happen.

Second, and no disrespect to 'el_supremo', your solution is fine, but there is a better way, to replace the transparent colors with a specific background, and is actually the same operation. -flatten the image. IM creates the right sized canvas and uses the default 'over' composition to overlay the image onto it. Just the one image.

Code: Select all

convert png.png -resize 500x400  -background white -flatten  png_small.jpg
See: http://www.cit.gu.edu.au/~anthony/graph ... s/#flatten
There are also otherways to do this , including adding a zero width border!

ASIDE: el_supremo may also have forgotting that resize may not produce the size requested as it preserves the aspect ratio. If you catually what a final image that is exactly that size 'pad out' the image as pure the IM example thumbnail section.

Re: Converting transparent PNG to Jpeg

Posted: 2007-02-12T03:00:28-07:00
by cs_rol
thanks, this was exactly what I was looking for.

Re: Converting transparent PNG to Jpeg

Posted: 2010-07-08T15:16:36-07:00
by qubodup
Thanks, this helped us fix the preview conversion on our site!

The perlMagick code we had to add to the conversion process was:

Code: Select all

$image->Set(background=>'white');
$image = $image->Flatten()