PNG compression levels

Ask for help and post your question on how to use XnView Classic.

Moderators: XnTriq, helmut, xnview

Post Reply
pshoplifter

PNG compression levels

Post by pshoplifter »

I always thought PNG compression was lossless, like compression with ZIP, and not lossy like with JPG.
How should I understand the compression levels for PNG?
I think 0 means no compression at all, and 9 means maximum compression, but is compression at level 9 still lossless?

It's very important for my work to know this for sure.
Photoshop does not give any options for PNG compression levels, but seems lossless, and it definately compresses.

Maybe someone has a relevant link to levels according to the officil PNG standard?

thanks!
Xyzzy
Posts: 652
Joined: Tue Nov 23, 2004 10:17 pm
Location: Poland

Post by Xyzzy »

For ZIP you can also specify compression and the compression is lossless.
If you want to be 100% sure: compress one file to PNG with different compression settings. Load them in Photoshop, make arithmetical subtraction on them and count colors. You should get 1, that means images are identical.

X.
User avatar
Drahken
Posts: 884
Joined: Sun Apr 10, 2005 4:29 pm

Post by Drahken »

#1) All PNGs are lossless.
There are a few people tinkering with the idea of lossy PNGs, but even these actually result in lossless files, they just apply some lossy filters before saving. All PNGs are compressed with the zlib deflate format, which is very closely related to ZIP, and is completely lossless.

The only PNGs that have any data loss are 256 color/indexed PNGs. These PNGs are reduced to 256 colors before saving (just like GIFs), but are otherwise completely lossless (again, just like GIFs).

2) The compression setting in PNG is merely a tradeoff between filesize and speed. You should never use 0 compression, as it does not decrease compression time at all vs a setting of 1 (technically, it would have to, but it would only be in the range of about 1 bilisecond), yet results in a massive file. (0 level PNGs are slightly larger than even the bloated BMPs, because they also have no compression, plus they have some extra headers.) Level 6 is the standard/default setting, 9 gives slightly smaller files, but can be noticeably slower (especially on large images).


Hmm... I just ran a quick test to compare file sizes with different compression settings, and found that level 3 was actually smallest. It made a big jump in size from 3 to 4, and never went back down quite as far, even up to level 9. I wonder if it's related to xnview (not all programs handle PNGs the same. Photoshop for instance, is known to create bloated PNGs), the image itself, or if this always holds true. I will have to run some more experiments with different images and differe programs and see what happens.

EDIT: I just ran some tests, and it's related to the specific image. Complex, photographic-type images achieve the smallest size at a compression level of 3, then jump at level 4. Simpler, graphic-type images follow the expected pattern and achieve their smallest size at level 9. (Some achieve the smallest size at level 4 and then remain at that size from 5-9, some continually get smaller until they reach level 9, but none make that sudden jump that the photographic images do.)
marsh
XnThusiast
Posts: 2443
Joined: Sun May 15, 2005 6:31 am

Post by marsh »

I get similar results to Drahken using Info-zip on a BMP image. So if PNG is similar, everything here looks normal to me. For my complex photo, "3" was highest compression. For my 256 color map, "7"&"9" were equal in highest compression.
User avatar
Drahken
Posts: 884
Joined: Sun Apr 10, 2005 4:29 pm

Post by Drahken »

While it's understandable that compression methods won't as well on some imagesas it does on others, it's strange that higher compression would actually add to the filesize. I wonder what changes in the compression method from level 3 to level 4 that makes it jump in size like that?
andreasm82
Posts: 36
Joined: Tue Feb 21, 2006 7:58 pm
Location: Saarland
Contact:

Post by andreasm82 »

I can enter numbers bigger than 9 in that compression ratio field... but it's only possible to select compression levels from 0 to 9 with that 2 arrows on the right side.

There's no warning or anything else if I type any number, e.g. 20 in that field. It saves the PNG file ...


Is that a bug ?
My private (german) page www.energie-bewusstsein.de
User avatar
Drahken
Posts: 884
Joined: Sun Apr 10, 2005 4:29 pm

Post by Drahken »

PNG compression only goes up to level 9. Presumeably, when you enter something higher than that, it just saves it at level 9.
Oh the feuhrer, oh the feuhrer, oh the feuhrer's nipples bonk!
foxyshadis

Post by foxyshadis »

I know you probably don't really care anymore ;) but the reason it gets larger after the cutoff between 3 & 4 is that it switches to a different prediction filter. PNG defines a half-dozen, and apparently whichever xnview uses at 1-3 is much better than the others, for photos. Some software like optipng tries all the filters at level 9 to find the smallest - obviously the most extreme tradeoff. :p It'd be cool if xnview could guess the right filter by quickly analysing the image, but that's not so easy.

Most of the software that let you pick a filter manually are command-line, plus Photoshop 5.x to my knowledge.
User avatar
budz45
XnThusiast
Posts: 1621
Joined: Sun Jun 03, 2007 6:05 pm
Location: UK

Re:

Post by budz45 »

Drahken wrote: 2) The compression setting in PNG is merely a tradeoff between filesize and speed. You should never use 0 compression, as it does not decrease compression time at all vs a setting of 1 (technically, it would have to, but it would only be in the range of about 1 bilisecond), yet results in a massive file. (0 level PNGs are slightly larger than even the bloated BMPs, because they also have no compression, plus they have some extra headers.) Level 6 is the standard/default setting, 9 gives slightly smaller files, but can be noticeably slower (especially on large images).
Drahken, thank you very much for your input on this topic it just helped me a lot with something. 8)

Please excuse me for bumping this old topic.
All My Topics || my 'MP' Topics
My own Bookmarked topics--->for me only
neher
Posts: 4
Joined: Fri Dec 14, 2007 12:27 am

PNG compression levels revisited in 2011

Post by neher »

Today saved a black on white diagram (1150 x 775 pixels) - with some halftone edges - and found that contrary to posts dating from five years ago the file now indeed gets smaller with each higher compression level. The original file at compression 0 is 2600 kB, level 1 reduces that to 137 kB, the default level 6 to 115 and the top level 9 to 112 kB. The file is too small to notice any difference in processing time.

Repeated the exercise with a photo, the original jpg is 3450 x 2600 pixels, saved by camera as 4000 kB; PNG level 0 = 26,300 kB / 3 seconds, level 1 = 11,700 kB / 4 seconds, level 9 = 9,900 kB / 38 seconds and level 5 = 10,400 kB / 9 seconds. Here processing time becomes significantly longer the more one compresses.

For comparison, saving the photo as a jpg at 80% with the 'slowest = best' algorithm takes just 2 seconds and results in a file of 975 kB - less than 10% of the PNG filesize.

For me, the preferred default would be level 3 = 11,000 kB / 6 seconds rather than level 6 = 10,200 kB / 12 seconds - it halves the time, while the resulting file is (only) 8% larger, which is not much considering download speeds available over the internet and how the price for huge capacity harddisks has fallen to the order of 10 cents per GigaByte. In addition, if I really wanted to economise on size and processing time the jpg option would be a huge leap further.

One wonders whether there would be an option to include a bit of explanatory text on the save > options page, perhaps a table indicating how small and how long a line diagram and a photo would take at each setting, and especially stating that all you're selecting is the compression level?

May I conclude this post with thanks to Pierre Gougelet for incorporating the 'better' algorithm which posters mentioned in 2006, and his ongoing development work in general?
User avatar
Drahken
Posts: 884
Joined: Sun Apr 10, 2005 4:29 pm

Re: PNG compression levels

Post by Drahken »

Today saved a black on white diagram (1150 x 775 pixels) - with some halftone edges - and found that contrary to posts dating from five years ago the file now indeed gets smaller with each higher compression level.
In the edit in my earlier post, I clarified that the jump only happened with photo type images, not graphics type ones.

One wonders whether there would be an option to include a bit of explanatory text on the save > options page, perhaps a table indicating how small and how long a line diagram and a photo would take at each setting, and especially stating that all you're selecting is the compression level?
If you export as PNG as opposaed to save/save as, you can choose filters manually.
Oh the feuhrer, oh the feuhrer, oh the feuhrer's nipples bonk!
Post Reply