2

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.

brush51
  • 5,691
  • 6
  • 39
  • 73
  • 1
    You need to make clear, do you mean you want to reduce the size of the image, or the size of the file? (Keep in mind that any *file* compression must be done using an algorithm which can be recognized by the reading app.) – Hot Licks Feb 08 '13 at 22:10
  • 1
    Do you know the method used by tinypng.org ? My suggestion is to first understand what is done, how to do it in any language/library, and only then start worrying about how to implement it specifically in objective-c. It performs some form of quantization, but the exact details are not available anywhere. That means we also cannot know whether the method produces bad results for some given input without uploading a lot of varied png images to the service and checking the result. Finally, this transformation do affect image quality. You cannot perform a lossy compression that does not. – mmgp Feb 08 '13 at 22:38
  • no, i dont know the method of tinypng.org. i just want to compress without seeable losts of image quality. on tinypng.org i have read that they use a method which is NOT available in photoshop and so on. – brush51 Feb 08 '13 at 22:42
  • Study up on lossless vs lossy compression and get back with us. – Hot Licks Feb 08 '13 at 22:53

3 Answers3

2

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

Community
  • 1
  • 1
max_
  • 24,076
  • 39
  • 122
  • 211
  • Why are you assuming it uses HSV ? It could just as well Lab, HSB, HSI, YCbCr, and etc etc, and even RGB. None of these links tell how tinypng.org does it, of course. There too many ways to perform quantization to try to guess what is done exactly by that service. – mmgp Feb 08 '13 at 22:59
  • Most references I have visited refer to the algorithms using HSV and RGB, that's why I'm assuming. Furthermore, yes, of course the links do not tell how tinypng actually does it, I didn't say that they did. – max_ Feb 08 '13 at 23:00
1

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

mmgp
  • 18,901
  • 3
  • 53
  • 80
1

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

foundry
  • 31,615
  • 9
  • 90
  • 125
  • Can you produce a good quantized png from http://i.stack.imgur.com/eJcam.png using ImageMagick ? I might include the best one I can get in the answer I've included here, but it is a bad result. – mmgp Feb 09 '13 at 16:16
  • @mmgp, point taken, so far my results are fairly poor in comparison with tinypng... – foundry Feb 10 '13 at 01:51