How to reduce png file size

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
robsch
Posts: 9
Joined: 2012-07-09T05:36:13-07:00
Authentication code: 13

How to reduce png file size

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to reduce png file size

Post by fmw42 »

User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to reduce png file size

Post by glennrp »

Right, if you can live with 8-bit precision, just put "-colors 255" before the "out.png" in your command.
robsch
Posts: 9
Joined: 2012-07-09T05:36:13-07:00
Authentication code: 13

Re: How to reduce png file size

Post 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?
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to reduce png file size

Post 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.
robsch
Posts: 9
Joined: 2012-07-09T05:36:13-07:00
Authentication code: 13

Re: How to reduce png file size

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to reduce png file size

Post 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
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: How to reduce png file size

Post 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.
Post Reply