Compress Images Using the Command Line
One of my favorite tricks to save time as a web developer and photographer is to use the TinyPNG API to compress images using the command line. Using the command line to compress images is a great way to compress images in bulk or to automatically compress images in an application that you are developing.
This tutorial requires installing this node-tinypng package.
Install Node.js and NPM
The first step will be to install the TinyPNG package for Node.js. This will require that Node.js and NPM are installed. Run this command to check if NPM is installed:
If the result is command not found: node then you need to install Node.js and NPM. This can be done by downloading the package here.
Install the Node.js TinyPNG Package
Once NPM is installed, the TinyPNG package can be installed. Run this command to install it.
sudo npm install -g node-tinypng
Get an API key from TinyPNG and set it as the default key.
tinypng -k YOURAPIKEY
That should be it! You should be ready to start using TinyPNG through Terminal.
Using the TinyPNG Node Package
Using the API is very easy. It can be used to compress a single image or multiple images. This API works for PNG and JPG images.
Compress One File
tinypng -r -n file.png
Compress All JPG Files Within A Directory
tinypng -r -n *.jpg
Hopefully, you find this tool as useful as I do. I have written about a few more handy tips for managing images using Terminal here.