brush51
brush51

Reputation: 5781

How to compress images (png, jpg and so on) using objective C

i want to shrink png or jpg on OSX. i only want to shrinkg without affecting the image quality.
like tinypng.org

is there any recommended library? i just know imagemagick. is there a way to do that natively? or another library to shrink/compress images without affecting the image quality?

my aim is to shrink the file size, for example:

logo.png >> 476 k before shrink 
logo.png >> 50k after shrink

Edit: to be clear, i want to compress the size of the file, not the image resolution.

Upvotes: 2

Views: 2787

Answers (3)

mmgp
mmgp

Reputation: 19241

My suggestion is to use http://pngnq.sourceforge.net, it will give better results than ImageMagick and for the single example given in http://tinypng.org, it also produces a very similar output. It is a tiny C implementation of the method present in the paper "Kohonen Neural Networks for Optimal Colour Quantization". That alone is much better since you are no longer relying on closed unknown implementations.

Original (57 KB), tinypng.org (16 KB), pngnq (17 KB):

enter image description here enter image description here enter image description here

Using ImageMagick, the best quantization to 256 colors I can get uses the LAB colorspace and dithering by Floyd-Steinberg:

convert input.png -quantize LAB -dither FloydSteinberg -colors 256 output.png

This produces a 16 KB png, but it contains much more visual artifacts:

enter image description here

Upvotes: 2

foundry
foundry

Reputation: 31745

Did you have a problem using ImageMagick? It has a rich set of quantize functions such as

bool MagickQuantizeImage( MagickWand mgck_wnd, 
                          float number_colors, 
                          int colorspace_type, 
                          float treedepth, 
                          bool dither, 
                          bool measure_error )

Here is a very thorough guide to quantization using imageMagick

Upvotes: 1

max_
max_

Reputation: 24521

TinyPNG.org works by using image quantisation - the similar colours in the image are converted into a HSV or RGB model and then merged depending on the distance.

How does it work?
...
When you upload a PNG (Portable Network Graphics) file, similar colours in your image are combined. This technique is called “quantisation”
...
src: http://tinypng.org

An answer here outlines a method of doing so: https://stackoverflow.com/a/492230/556479.

There are also some answers on this question with refer to how you can do so on Mac OS using objective-c: How do I reduce a bitmap to a known set of RGB colours

See Wikipedia for a more in depth guide: http://en.wikipedia.org/wiki/Color_quantization

Upvotes: 2

Related Questions