TinyPng is a nice service which provides you 500 free image compressions a month. And this is nice, since in a typical Android project, you wouldn’t have more than 500 images in any given month :). I have a simple script that scans through all your images, and tries to compress them using the tinypng api. All you need to do is to copy the script, put it in the root folder of your project and run it.
Head over to TinyPng website to checkout how it works.
What does TinyPNG do?
TinyPNG uses smart lossy compression techniques to reduce the file size of your PNG files. By selectively decreasing the number of colors in the image, fewer bytes are required to store the data. The effect is nearly invisible but it makes a very large difference in file size!
Advanced lossy compression for PNG images that preserves full alpha transparency.
Let’s jump into it. This script will search for all the images(pngs) in your Android project, pick each one, call the API, and overwrite the images, with a compressed version. And since this is a lossless compression, you won’t notice any change in quality. The overwriting part is probably a bit dangerous. So, with a little modification, you could change the script so that it creates a copy of the files.
The initial script for tinypng was taken from this gist by rakuishi. Thanks :).
- The first thing you would need to do is to get an API key here.
- Copy this file to the root of your android folder.
- Replace your API key in this file on the top.
- Run “ruby imgcrush.rb”
- View the generated HTML report. (Link to sample report)
That’s it. For an example above, you could see, the script reduced the size to almost half for each image. And the total savings is somewhere about a whooping 53%.
Note: There are other image compression APIs/tools which I haven’t tried yet. Android build system itself has a pngcrusher which tries to optimize all your assets.