TinyPNG is an online tool for optimising PNG images and shrinking the file size. Its compression techniques are magical and result in a massively decreased file size without any noticeable visual difference. The website allows you to optimise images by dropping in up to 20 images at a time
TinyPNG also offers a developer API and there are a number of available tools which utilise this powerful compression engine. One of these tools is a very nice command line utility developed by websperts which will allow you to shrink PNG images directly from the terminal.
This guide will run you through the installation of TinyPNG CLI and demonstrate a few examples of it in action.
Before getting started you need to obtain a TinyPNG API key. You can request a free API key from the developers page which will allow you to optimise up to 500 images per month. They just require a name and an email address and a key will be emailed out to you.
tinypng-cli is currently available as a Node.js package.
In order to install this tool you should make sure that Node is installed on your system along with the Node Package Manager. npm is included by default with the main Node installation, check out the downloads page to find the right installation details for your platform.
For OSX users, Node can also be installed with Homebrew:
brew install node
To install, simply run this command:
npm install -g tinypng-cli
This will download and install via the Node Package Manager.
To activate tinypng-cli you need to provide your API key. This can be done by saving your key to a file called .tinypng and placing it within your home directory.
To find the location of your home directory you can enter this command:
The following command is an example of how to quickly generate the required file containing your key.
echo "SqGmZLREd10xw66-vsOiWOxCuU3-nEIi" > .tinypng
Now we are ready to have some fun. To optimise a single PNG image, pass the image path to tinypng. Here I am passing an image which is 1mb in size.
TinyPNG will then process the file and let you know the results.
TinyPNG just reduced my file down to 328KB, a very nice optimisation result.
To process multiple images at a time you can also pass multiple paths:
To process all PNG images within the current directory you can run either one of these commands:
To recursively process PNG images within the current directory and all subdirectories, use the -r flag:
For more usage examples, check out the official github repository.
Hopefully this article can encourage you to try out the TinyPNG service and take advantage of its power from your terminal.