How to optimize images by using macOS Terminal + TinyPNG CLI

A short guide on how to optimize images by using the macOS Terminal and TinyPNG CLI npm package

Sergey Nikishkin
Acronis Design

--

Let’s imagine that you have required to optimize a bunch of images. Let’s say 50 — 100 or even more at one time. Your actions? Of course, the first step will be looking for possible solutions on Google. And of course, you will find many online optimizers that working well in the browser.

But all of them will have some restrictions like the number of uploaded images at one time or file size limits, or something else.

And don’t forget that you should upload files for optimizing first and after it downloads optimized files back. How many times you will repeat this action with 100 images and restrictions that have online tools? The answer is simple. Many times.

So, and what is the solution?

One of the possible solutions is to use the TinyPNG CLI npm package and do all the optimizations locally in the Terminal.app by using one simple command after the environment will be set up. No upload/download file actions, no file size restrictions. But why TinyPNG? In my opinion, this is one of the best optimizing tools that provide very good image optimization.

Setting up the environment

Before we start, you should have the following things installed on your Mac:

After Command Line Tools and Node JS will be installed, let’s install the required npm package. Just open the Terminal app and execute the command:

npm install -g tinypng-cli

This command will install the package globally. It means that you will be able to optimize images in any directory on your device.

API key

Because you will use a third-party API, before start shrinking images you should get the unique API key that will open the access to the required functionality. Also, keep in mind that TinyPNG has a limit on 500 free images per month. And all optimizations over this limit will cost money. So, to get the key, follow the steps below:

It’s time for optimization

As an example, let’s take 120 files with a total size of more than 500 MB and will try to shrink them all at one time.

Open the Terminal and drag there your images folder with the pressed cmd button. But don’t forget that all images in the specified folder will be rewritten by optimized ones. So, let's optimize all these images by executing one simple command:

tinypng -k your_API_key

The command execution had taken about 2 minutes and now we can check the results. As you can see in the screenshot below, the total size of the images after optimizing became smaller almost 10 times.

As a bonus

You can not only optimize images but also, easily resize them by width or height by executing one of two more commands:

tinypng --height 700 -k your_API_keytinypng --width 700 -k your_API_key

In conclusion

I have shown only one of the possible ways on how to use TinyPNG API. But you can choose your own method from the huge list of the third-party solutions that TinyPNG provided. Extensions, plugins, libraries, Automator scripts, and even desktop apps. Just check the whole list here:

https://tinypng.com/third-party

Follow Acronis Design on

--

--