imagemagick resizing and quality PNG
Asked Answered
G

5

32

In my application I need to resize and make the quality on PNG files poorer.

In full size the PNGs are 3100x4400px using 2,20MB disk space.

When running the following command:

convert -resize 1400 -quality 10 input.png output.png

the images are resized to 1400x2000 using 5,33MB disk space.

So my question is: How can I reduce the file size?

Goodsell answered 27/10, 2011 at 8:28 Comment(1)
You can resize until you get a small enough file size. You can limit the number of colors and save as palette 8bit color. (see imagemagick.org/script/command-line-options.php#colors and imagemagick.org/Usage/formats/#png_formats). You can posterize the colors. You can use different compression types/values (see imagemagick.org/script/command-line-options.php#quality), though that will not likely make that much difference.Hippodrome
R
20
  1. You can further reduce quality of PNG by using posterization:

    https://github.com/pornel/mediancut-posterizer (Mac GUI)

    This is a lossy operation that allows zlib to compress better.

  2. Convert image to PNG8 using pngquant.

    It reduces images to 256 colors, so quality depends on the type of image, but pngquant makes very good palettes, so you might be surprised how often it works.

  3. Use Zopfli-png or AdvPNG to re-compress images better.

    This is lossless and recommended for all images if you have CPU cycles to spare.

Radiogram answered 17/12, 2011 at 18:38 Comment(4)
You can also use convert source.png png8:dest.png to convert to PNG8Esbensen
ImageMagick's converter is pretty bad and doesn't support transparency properly: imagemagick.org/discourse-server/viewtopic.php?t=23340Radiogram
is there any way to generate png with specific filesize limit like jpg -define jpeg:extent=70KBAccelerant
No, PNG in general is not suitable for compressing for a certain file size, so the only thing you can do is to compress multiple times with different settings and pick one of the results that is closest to the size you want. But compressing to a fixed file size is most of the time a very bad idea, since it will give you both too-ugly quality on complex files, and waste space on simple images.Radiogram
M
14

After using imagemagick to resize, you can compress the image using pngquant.

On mac (with homebrew) brew install pngquant then:

pngquant <filename.png>

This will create a new image filename-fs8.png that is normally much smaller in size.

Mongoose answered 15/8, 2018 at 17:37 Comment(1)
That difference is massive! Just tried it out after using convert to shrink some PNGs - pngquant took the images from ~550KB to ~170KB. Wow!Eparch
T
7

For lazy people that arrived here wanting to paste in a one liner:

mogrify -resize 50% -quality 50 *.png && pngquant *.png --ext .png --force 

This modifies all of the pngs in the current directory in place, so make sure you have a backup. Modify your resize and quality parameters as suits your needs. I did a quick experiment and mogrify first, then pngquant, resulted in significantly smaller image size.

The ubuntu package for pngquant is called "pngquant" but I had it installed already on 20.04LTS so seems like it may be there by default.

Twandatwang answered 29/9, 2021 at 0:8 Comment(1)
Great. Also worked like a charm on a Mac machine. I just used a little bit more quality, since 50% is tooo much. anyway, anybody is free to play with theirs.Roodepoortmaraisburg
P
6

Help page says, that -quality option used with PNG sets the compression level for zlib, where (roughly) 0 is the worst compression, 100 - is the best (default is 75). So try to set -quality to 100 or even remove the option.

Another method is to specify PNG:compression-level=N, PNG:compression-strategy=N and PNG:compression-filter=N to achieve even better results.

http://www.imagemagick.org/script/command-line-options.php#quality

Priscian answered 27/10, 2011 at 9:1 Comment(0)
G
-4

I found that the best way was to use the

- density [value] 

parameter.

Goodsell answered 27/10, 2011 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.