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
Converting transparent PNG to Jpeg
-
- Posts: 1015
- Joined: 2005-03-21T21:16:57-07: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.
This creates a white canvas and overlays the png image on top so that it has the background you want.
Pete
Code: Select all
convert png.png -resize 500x400 -size 500x400 xc:white +swap -compose over -composite png_small.jpg
Pete
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
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.
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.
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
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/
https://imagemagick.org/Usage/
Re: Converting transparent PNG to Jpeg
thanks, this was exactly what I was looking for.
Re: Converting transparent PNG to Jpeg
Thanks, this helped us fix the preview conversion on our site!
The perlMagick code we had to add to the conversion process was:
The perlMagick code we had to add to the conversion process was:
Code: Select all
$image->Set(background=>'white');
$image = $image->Flatten()