Page 1 of 1

How to reduce png file size

Posted: 2012-10-21T04:49:58-07:00
by robsch
Hi,

I have a convert command that produces a png file which is too big for my purposes. I get files with more than 200 kb but it should be less than 50 kb, if this is possible. How do I have to modify the parameters? What can I do? The quality of the image should be as close as possible to the original file. Note: the in.png could be also an in.jpg (or other supported formats). All works fine so far. Just the file is too big.

Code: Select all

/usr/bin/convert -size 160x160 xc:none \( 
  "in.png" -strip -thumbnail "140x140^" -gravity center -crop 140x140+0+0 +repage \( 
      +clone -fill black -colorize 100% -fill white -draw "translate 70,70 circle 0,0 70,0" 
  \) 
  -alpha off -compose copy_opacity -composite 
\) 
-alpha Set -compose src-over -composite -fill none -stroke "#0000FF" -strokewidth 10 -draw "translate 79.5,79.5 circle 0,0 74,0" "out.png"
What the command does: From the source image it creates an ruond image that will have a certain width and heigth, surrounded by a round border. It could be that this is not perfect, but it currently does what it is ougth to do. Must it be different in order to reduce the file size?

Hope, anyone can help me (dummy) with this.

Thanks und regards,
Robert

Re: How to reduce png file size

Posted: 2012-10-21T12:31:45-07:00
by fmw42

Re: How to reduce png file size

Posted: 2012-10-21T12:49:20-07:00
by glennrp
Right, if you can live with 8-bit precision, just put "-colors 255" before the "out.png" in your command.

Re: How to reduce png file size

Posted: 2012-10-22T02:43:18-07:00
by robsch
Thank you for your help.

I think 8-bit is not appropriate since in.png is a photo. Also the round border should be smooth. At least it shouldn't be gif-quality. (Sorry, I don't know the terms, but is it the alpha channel that is required?)

What is interesting: I have opened the created out.pnp in Photoshop. And there, if I do 'Save for Web and Devices...' (or the like) I can reduce the size of the file from 226 to 82 kb without any quality loss. The original and the optimized image look identic.

So it seems that the image created by IM has some extra data that is not required. At least for me. But I don't know what it is.

Do you have any idea?

Re: How to reduce png file size

Posted: 2012-10-22T05:55:14-07:00
by glennrp
robsch wrote:Thank you for your help.

I think 8-bit is not appropriate since in.png is a photo.
Chances are you are actually writing a 16-bit per sample (64-bit pixels) PNG.
Try putting "-depth 8" immediately ahead of "out.png". This will give you
8 bits per sample (32-bit pixels) which should be good enough. Alternatively,
use "png32:out.png" which will always write a 32-bit RGBA PNG even if the image
contains 256 or fewer colors.

Re: How to reduce png file size

Posted: 2012-10-22T07:59:43-07:00
by robsch
Hi glennrp,

yes, that did it! Puh... I'm glad. Now my images are only about 1/3 of the original size. This is much more 'realistic'.

I do not really understand what happens. Anyway, many thanks for your solution!!! :D

Re: How to reduce png file size

Posted: 2012-10-22T10:04:32-07:00
by fmw42
If there are profiles and other meta data, that often can be large, and which you do not need, you can try removing them with -strip and or +profile *

http://www.imagemagick.org/script/comma ... .php#strip
http://www.imagemagick.org/script/comma ... hp#profile

Re: How to reduce png file size

Posted: 2012-10-22T10:45:45-07:00
by glennrp
robsch wrote: yes, that did it! Puh... I'm glad. Now my images are only about 1/3 of the original size. ...
I do not really understand what happens.
I think it was the "-draw circle" operation that forced everything to 16-bit precision. The problem wasn't meta data
because your "-strip" directive had already removed those.