A PNG is a compressed and lossless image format.

Here information about PNG compression can be found: http://optipng.sourceforge.net/pngtech/optipng.html

See also this question: https://superuser.com/questions/845394/how-is-png-lossless-given-that-it-has-a-compression-parameter

How can I set the compression degree (1 to 9) when exporting an image to PNG format?

I saved a bitmap Image using XnView with different compression degrees to png format. It seems that Mathematica is using as most of the software a default value of 6.

  • 2
    $\begingroup$ Like you said, PNG is lossless so what exactly does "compression degree" mean? For a lossy format it makes sense, you basically specify how much information you are willing to throw away, but with lossless formats... $\endgroup$
    – C. E.
    Mar 12, 2017 at 9:10
  • $\begingroup$ @C. E.: If you look here en.wikipedia.org/wiki/Portable_Network_Graphics you will find the info: "PNG uses a non-patented lossless data compression method known as DEFLATE, which is the same algorithm used in the zlib compression library". You can use e.g Xnview, Photoshop or any other Image software and you will find the possibility to set the compression degree factor. It means that for 9 the file size is smallest and no information is lost due to compression, that's what I want. $\endgroup$
    – mrz
    Mar 12, 2017 at 9:29
  • $\begingroup$ ok, so the answer is that in a lossless format there is a slight trade-off between computation speed and compression. But the next sentence indicates that it is usually not important: "Compared to formats with lossy compression such as JPG, choosing a compression setting higher than average will delay processing but often not result in a significantly smaller file size." From my reading of the documentation, there is no way to manage this trade-off. Perhaps someone else will know. $\endgroup$
    – C. E.
    Mar 12, 2017 at 9:39
  • $\begingroup$ @C. E.: In science where images are recorded with cameras of different types, it is important not to loose any information while saving. And what we also wish is not to waste hard disk space (each original pixel value is important). Therefore we use png with compression. Please see also: howtogeek.com/203979/…. I only want to know if Mathematica can set this compression degree. $\endgroup$
    – mrz
    Mar 12, 2017 at 10:06
  • $\begingroup$ I don't understand why you link me to a website that says exactly what I said. You didn't make it clear in your comment or in your question, so I just wanted to clarify what "compression degree" means for a lossless format. $\endgroup$
    – C. E.
    Mar 12, 2017 at 10:27

1 Answer 1


There is no documented way to do this within Mathematica, as you can check yourself in the documentation.

There are free tools like optipng and pngcrush that you can use to recompress an existing PNG file to a smaller size. You can call these directly from within Mathematica using RunProcess and related functions.

  • $\begingroup$ Do we know what degree is used by default? $\endgroup$
    – george2079
    Mar 12, 2017 at 17:16
  • $\begingroup$ @george2079 I wonder if that question makes sense at all. I always thought that these "degrees" are specific to the compressor that is being used, and that there is nothing in the resulting compressed stream that would let you decide what "compression level" it has. But I may be wrong. Mark Adler would know. $\endgroup$
    – Szabolcs
    Mar 12, 2017 at 18:49
  • $\begingroup$ i guess right question is, 'do the re-compression tools mentioned provide a worthwhile size reduction over what you get from export?' $\endgroup$
    – george2079
    Mar 12, 2017 at 20:53
  • $\begingroup$ @george2079 Oh yes, they do. I usually get between 10%-50% reduction for PNGs exported by Mathematica. $\endgroup$
    – Szabolcs
    Mar 12, 2017 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.