PNG file grows dramatically when image size reduced

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
lenscape

PNG file grows dramatically when image size reduced

Post by lenscape »

This issue arises from a question in the freenode IRC forum: ##imagemagick.

A screenshot taken with KDE ksnapshot produces a png image of 327KB. The following command:

Code: Select all

convert snapin.png -resize 80% snapout.png 
successfully reduces the image dimensions to 80% but increases the file size to 1.1MB.

Gif output, on the other hand, is 204KB.

Why this huge increase in file size for png output and what can I do about it?

Thanks.

Imagemagick 6.3.0
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: PNG file grows dramatically when image size reduced

Post by Bonzo »

Somebody I know was having the same problem with from memory the Serif gallery. I think the problem is down to the amount of colours ?

I have just done a test:

Original image
Colors: 44713
Rendering intent: Undefined
Resolution: 72x72
Units: PixelsPerInch
Filesize: 194.854kb

Resized png
Colors: 74088
Rendering intent: Undefined
Resolution: 72x72
Units: PixelsPerInch
Filesize: 384.553kb

Thumbnail png
Colors: 74088
Rendering intent: Undefined
Resolution: 28.34x28.34
Units: PixelsPerCentimeter
Filesize: 377.094kb

Resized gif
Colors: 256
Rendering intent: Undefined
Resolution: 72x72
Units: Undefined
Filesize: 57.8711kb

Thumbnail gif
Colors: 256
Rendering intent: Undefined
Resolution: 72x72
Units: Undefined
Filesize: 57.8711kb


Resized png maximum amount of colours set to 500
Colors: 500
Rendering intent: Undefined
Resolution: 72x72
Units: PixelsPerInch
Filesize: 242.129kb
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: PNG file grows dramatically when image size reduced

Post by anthony »

Bonzo, you are quite right that the large increase in the number of colors is the cause. More colors ganerally makes the image harder to compress.

Reducing the number of colors will naturally then help.

However you can probably reduce the resulting size even further by using a ordered dither of the fewer colors. Normal IM color reduction (actually pretty wll all graphic editors) uses a error-correction dither to reduce to arrange the number of colors in a psuedo-random pattern, to simulate the original colors of those areas of the image.

But the psuedo-random pattern itself does not compress well!

ordered dither paterns do compress well! But currently it can not use the 'best' colors found in the image by color quantization, only mathematical colormaps (like you get from -posterization).

It is posible to generate a ordered-dirther using any set of colors. But the algorithm is elusive, any complex. When I figure it out, we may even be able to compress images better than what you are getting!

For examples of using order-dither to reduce the overall size of images, see 'Video to Gif Color Optimization' .

If anyone can help with the above. Please mail me.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply