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?".
Post Reply
cs_rol
Posts: 22
Joined: 2007-01-16T04:40:08-07:00

Converting transparent PNG to Jpeg

Post 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
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
cs_rol
Posts: 22
Joined: 2007-01-16T04:40:08-07:00

Re: Converting transparent PNG to Jpeg

Post by cs_rol »

thanks, this was exactly what I was looking for.
User avatar
qubodup
Posts: 31
Joined: 2009-10-07T13:11:41-07:00
Authentication code: 8675309

Re: Converting transparent PNG to Jpeg

Post 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()
Post Reply