Convert image to uncompressed PNG from the command-line

16,044

Solution 1

ImageMagick will always compress PNG files. The worst compression you can get is using:

convert -verbose -quality 01 input.png output.png

but it depends on the image content (the 0 will use Huffman compression which sometimes compress better than zlib).

You can try other tools like pngcrush (http://pmt.sourceforge.net/pngcrush/) to disable the compression:

pngcrush -force -m 1 -l 0 input.png output.png

which create a file the same size GIMP create when using Compression Level 0 (few bytes more or less).

Some example sizes (for a photographic PNG, 1600x1200):

  • original: 1,693,848 bytes.
  • after IM: 2,435,983 bytes.
  • after GIMP: 5,770,587 bytes.
  • after pngcrush: 5,802,254 bytes.

Solution 2

To quote the ImageMagick doc: Not all combinations of compression level, strategy, and PNG filter type can be obtained using the -quality option. For more precise control, you can use the -define option.

For example, this command should create an uncompressed (RGB) PNG file:

convert INFILE \
-define png:compression-level=0 \
-define png:compression-filter=0 \
-define png:color-type=2 \
OUTFILE.png

You might get a warning, which is a harmless bug. It will still produce a correct png. http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=20257

Sample image:

112233 112233 112233 112233
445566 445566 445566 445566
778899 778899 778899 778899
aabbcc aabbcc aabbcc aabbcc

IDAT chunk of the PNG file created with IM:

49444154081d013400cbff00
11223311223311223311223300
44556644556644556644556600
77889977889977889977889900
aabbccaabbccaabbccaabbcc
7d6f14b9...

However, for certain cases (e.g. group compression) it could be worthwhile testing a format with less overhead, for example TARGA (.tga).

Solution 3

I like the netpbm suite of tools, it is designed in the UNIX "software tools" tradition: "write programs that do one thing and do it well".

You can do what you asked like this:

< in.png pngtopnm > image.pnm
< in.png pngtopnm -alpha > alpha.pnm
<image.pnm pnmtopng -alpha alpha.pnm -compression 0 > out.png
rm image.pnm alpha.pnm

You might lose certain metadata from the original png file.

It seems a bit complicated, due to the way we handle the alpha channel.

Netpbm is a toolkit for manipulation of graphic images, including conversion of images between a variety of different formats. There are over 220 separate tools in the package including converters for more than 80 graphics formats.

Share:
16,044
Mechanical snail
Author by

Mechanical snail

🐌. Native speaker of American English. Linux user. Familiar with several programming languages in the procedural, OO, and functional paradigms. Worst Code Golf ever New badge proposals! Shakespeare bug in Ubuntu The Great Question Deletion Audit of 2012 Chat Horrible spiders Adorable fluffy things LASERS! Link rot is evil. Archive everything. The keyboard is king. Correctness over performance. Canonicalize, normalize, deduplicate. Don't repeat yourself. UTF-8 &gt; UTF-16. Use static typing: good for tooling. Re-use; don't re-invent. Correctness, then clarity, then concision and elegance. Play devil's advocate. First understand opponents' positions.

Updated on September 18, 2022

Comments

  • Mechanical snail
    Mechanical snail over 1 year

    I have a compressed PNG image compressed.png. I can convert it to an uncompressed PNG decompressed.png using GIMP (saving as PNG and setting compression level to 0). How can this be done on the command line (Linux)?

    I recall doing this in the past using Imagemagick's convert, but I forgot how. I tried some things that I thought should work based on the documentation:

    • convert compressed.png -compress None decompressed.png
    • convert compressed.png +compress decompressed.png
    • convert compressed.png -quality 0 decompressed.png
    • convert compressed.png -quality 00 decompressed.png

    just wrote an ordinary compressed PNG.

    Aside: why would you want an uncompressed PNG?

    Some cases:

    • You want to support efficient (binary) diffs of the image data, while still using other features of the PNG format (as opposed to storing raw image data or BMP).
    • You want to compress several PNGs together in a tarball or 7z archive, but want to keep using PNG features. If the images are sufficiently similar this can give a better compression ratio than compressing individually.
    • Useful as a baseline size for testing PNG optimizers.
    • Mechanical snail
      Mechanical snail almost 12 years
      @MichaelHampton Edited with examples
  • Mechanical snail
    Mechanical snail over 11 years
    Didn't seem to work for me (ImageMagick 6.6.9-7 on Linux)—output is still compressed.
  • Meyer
    Meyer over 11 years
    I've tested it with ImageMagick 6.7.2-7. It seems that this key was added recently, so you will need to use a more up-to-date version of IM.
  • Mechanical snail
    Mechanical snail over 11 years
    imagemagick.org/script/command-line-options.php says "png:compression-level=value: valid values are 0 through 9, with 0 providing the least but fastest compression and 9 usually providing the best and always the slowest.".
  • Meyer
    Meyer over 11 years
    Sorry, I think I misunderstood your first comment. Looking at the actual bits, you are right, the image was still not a clean bitmap. Importantly, this was also the case with the GIMP generated version. However, further experimenting with the IM define option indicates that it still is possible. I edited the answer accordingly.
  • Ruslan
    Ruslan almost 3 years
    I still get compressed output (although the size is very similar to uncompressed) with ImageMagick 7.0.5-5. The pngcrush-based answer works well instead (though it requires a PNG input, which is not good when one wants fast conversion between e.g. BMP and PNG).