views:

505

answers:

4

Hello.

I'm using ImageMagick to convert some files from one format to another. I was always under the impression that .png files were supposed to be as big/small as .jpg if not smaller, and definitely smaller than .gif.

However when I run

convert photo.jpg photo.png 

The files I'm getting out is about 6 times bigger than the original jpg.
Original jpg is a regular photo about 300x500 px, 52 kb. Output is a proper png of the same dimensions, but size is about 307 kb?

Does anyoone know what the hack is going on? Am I doing something wrong?

P.S.:

I tried both on Debian and Windows with the same results.

P.P.S.:

Also when I add resize option to this and resize to 10000 x 10000. Converting and resizing to jpg takes a few seconds, but it works, if I do the same of png, I jsut strt running out of memory altogether

+6  A: 

JPG is a lossy compression algorithm while PNG is a lossless one.

This fact alone will (in general) make JPG images smaller than PNG ones. You can tweak the compression ratios for each format, so it could also be that your not compressing your PNG files as much as your JPG ones.

For a photographic image saving as JPG will usually produce a smaller file than PNG as there's more noise or randomness in the image for the compression to work with. Images created by graphic art tools will tend to have more hard edges which will compress better in PNG.

GIF is smaller because it's based on an colour palette (of 256 colours) rather than the separate RGB values for each pixel (or group of pixels) in JPG and PNG.

ChrisF
Chris, thanks for a speedy reply. I haven't done programmatic images processing before, and coming from gui products such as Photoshop and Paintshop, I'm used to having smaller pngs, since they use optimized pallets by default. Is there any way to get imagemagick to reduce quality in order to do conversion to png faster?
Nick Gorbikoff
@Nick - I don't know imagemagick so I can't say for sure, but there should be an "options" button somewhere, either on the save dialog itself or in the preferences.
ChrisF
No idea about imagemagick but any good image editor should have a function which reduces the number of colors used in the picture.
Bus
ImageMagick is a command line processing tool, so not button :-( Trying to read through docs right now but it's a pain.
Nick Gorbikoff
@Nick - told you I didn't know imagemagick ;)
ChrisF
+2  A: 

The .jpeg file format is a lossy format, it throws information away. Which makes for good compression ratios. The .gif file format is not lossy but loses other information, it only supports 256 colors. The .png file format is not lossy and preserves the color range.

Hans Passant
A: 

It depends on the kind of image. JPEG's lossy compression works very well on images with lots of color gradients (i.e. photos).

Try it on an image consisting of areas with the same color separated by sharp edges (screenshots are good, unless they show a photo desktop background, and the fancy window borders and task bar of Vista and Windows 7 also tend to mess this up). JPEG is bad at that kind of thing, and you'll probably find PNG to compress better (probably better than GIF too).

Michael Borgwardt
+2  A: 

The PNG format can provide images of 24 bits per pixel or 8 bits per pixel. JPG is a 24 bit format, but it uses lossy compression to reduce the file size significantly. PNG and GIF both use lossless compression, but GIF only works with 8 bits so it requires your image to have 256 colors or less - this often results in a grainier picture.

Try the -colors and -dither options in ImageMagick to reduce the number of bits in your PNG output. At that point it should be comparable to the GIF file size. If you need to reduce it further, there are utilities to optimize the PNG.

Mark Ransom
Mark thank you for advice, but even -colors 254 for png, produces and image bigger than jpeg. I guess I'll have to save everything as jpgs.Thank
Nick Gorbikoff
@Nick, one question - are your gifs also larger than the jpgs? If so, you've probably made the right choice. Png might be smaller than gif, but not by a huge margin.
Mark Ransom
yeah gifs are definitely larger - no surprise there, I just always thought that png would produce better results. But since I really don't strive for quality here, but rather for smaller size, jpg it is. Thank you
Nick Gorbikoff