Converting transparent PNG to Jpeg

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?".

Converting transparent PNG to Jpeg

Postby cs_rol » 2007-02-01T09:25:26+00:00

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
cs_rol
 
Posts: 20
Joined: 2007-01-16T04:40:08+00:00

Postby el_supremo » 2007-02-01T10:53:55+00:00

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
el_supremo
 
Posts: 746
Joined: 2005-03-21T21:16:57+00:00

Postby anthony » 2007-02-01T22:12:47+00:00

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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 4377
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

Re: Converting transparent PNG to Jpeg

Postby cs_rol » 2007-02-12T03:00:28+00:00

thanks, this was exactly what I was looking for.
cs_rol
 
Posts: 20
Joined: 2007-01-16T04:40:08+00:00

Re: Converting transparent PNG to Jpeg

Postby qubodup » 2010-07-08T15:16:36+00:00

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()
User avatar
qubodup
 
Posts: 4
Joined: 2009-10-07T13:11:41+00:00


Return to Users

Who is online

Users browsing this forum: Google [Bot] and 4 guests