Compress a PNG

I have seen online website that will reduce the filesize of a PNG with minimal loss of quality.

Does anyone have a Xojo (or Swift) method to do this?

If not I’ll just spend a few hours shuffling them to the web and back

Try ImageOptim

I was looking for a solution I could built into a project… I already have access to an app like this

I want to batch process a few hundred PNG files from start to fininsh without haveing to manually deal with a 2nd process…

I think OptiPNG can be ran on the command line or you could possibly integrate its source code into your project.

As I understand it, PNG files are compressed by default. Unlike JPEGs they can’t be compressed much further since they are lossless. Prolly your best option here is to convert the PNG into JPEG and save it at a lower “quality”.

Well that can’t be true… since I have used a web site that compressed a number of PNGs from 5K to 2K … with no visible loss of quality (they were just icons)

According to Wikipedia:

PNG uses a 2-stage compression process:

  • pre-compression: filtering (prediction)
  • compression: DEFLATE

Deflate compression is lossless, but it also has 10 levels of aggressiveness and several alternate compression strategies that can be much more effective on certain kinds of data than the default strategy.

I suspect that the trick to reducing PNG file size is to decompress the original PNG data first and then recompress it using a different strategy and at maximum aggressiveness.

Well, @prodman is right, you CAN’T crompress a PNG any more than what it is. What all this tools do is Re-encoding the images with some adjustments like Bit deph or the amount of colors, channels, etc. As this is a Re-Encodig with less data, there is allways a Lossy process

Maybe you don’t note, but is there.

As this is creating a whole new image with less quality (or at least different settings), you would need a full PNG implementation with acces to those settings, maybe the MBS Plugin for PNG.

But, even having this part solved, you still need to write some cleaver code to analize the image and decide wich settings will have the best ratio of quality/filesize.

I would use a 3rd party command line tool

Thanks… but I didn’t really care about the how/why… just if anyone had some code to do it…

seems not… although Sam Rowlands did mention something on an old post on TOF about some app he had of which this was a feature…

may be of some use by changing the compression level: GitHub - kelvin13/png: decode, inspect, edit, and encode png images in pure swift

1 Like

OptiPNG can often make PNGs smaller without losing quality by deciding it and then trying different methods to re-encode it.

That is true, there are afew real optimizers, but the problem with those is that they will tell you 99% of the time that your PNG is already optimized with normal settings. You have to use some of the aggresive methods to get the best optimization, this could take half a minute or even more with big files. And the reduction in the file size is very little.

You will see good results only if your PNG was made by a very crappy app that did no optimization at all.