OpenSource Commandline Alternative For TinyPNG To Compress Images Offline

Ashok Raja T
Technology Specialist
October 31, 2021
Rate this article
Views    4047

TingPNG is one of the popular online image compression utilities used by most web developers. If you are looking at an offline tool or if you are concerned about uploading the image to an external site for compression, then Squoosh, an OpenSource Node Js Application is the best alternative for Tiny PNG.

Squoosh CLI As An Alternative For TinyPNG

Image compression is the only functionality that Squoosh does and it does it well. It is an OpenSource application developed with NodeJs. NodeJs is a pre-requite for this application and if you haven’t already installed NodeJs, you can install it by downloading it from the NodeJs website.

If you wish to install multiple different versions of NodeJs, this link explains the steps. Those steps are for SUSE Linux, but it would work for most of the Linux Operating systems.

To install Squoosh CLI, execute the command npm i -g @squoosh/cli in terminal. This would install Squoosh and it’s dependencies in your local machine.

Similar to TinyPNG, Squoosh also has an online compression service that can be accessed at https://squoosh.app/

Squoosh CLI and Its Options

Let us see, how to perform some of the common use cases with Squoosh cli. I am using an image, with size 18.24MB size from Unsplash for this demo.

#Compress Image. It reduces the file size to 1.3 MB
squoosh-cli  --mozjpeg {quality:60} photo-1585818908378-6d9e9c51a913.jpeg

#Save Compressed Image In a different name
squoosh-cli  --mozjpeg {quality:60}  -s "_z"   photo-1585818908378-6d9e9c51a913.jpeg 

#Resize Image Width ( It adjusts the height proportionally ). Reduces the file size to 31KB
squoosh-cli --resize {width:700}  --mozjpeg {quality:60}  -s "_z"   photo-1585818908378-6d9e9c51a913.jpeg 

#Resize Image To Specific Size
squoosh-cli --resize '{width:250,height:250}'  --mozjpeg {quality:60}  -s "_250x250"   photo-1585818908378-6d9e9c51a913.jpeg

squoosh_cli

Global Function

You can also create a function with the below content in the .bash_aliases file. This would help to avoid passing the desired argument values again and again.

sqz () { 
    
    ARG1=${3:-_z}
    ARG2=${4:-60}

    if [ ! -z $2 ] 
    then 
        squoosh-cli  --mozjpeg {quality:$ARG2}  -s "$ARG1"   --resize {width:$2} $1
    else
        squoosh-cli  --mozjpeg {quality:$ARG2}  -s "$ARG1"  $1
    fi
    echo 'Completed !!!'
}

To call the above function, try the below commands.

# Call with the default option
sqz img.jpg

# Compress the image with default quality and resize the image to 700 px width
sqz img.jpg 700 

# Append the file name with _x
sqz img.jpg "" _x 

# Append the file name with _x and compress the image with 75 %  quality 
sqz img.jpg "" _x 75  

# Compress the image with 75% quality
sqz img.jpg "" "" 75  

# Compress the image with 75% quality and resize it to 700 px width 
# and rename the file by appending _new to the filename
sqz img.jpg 700 _new 75

Compressed Image

The below is the output of the image after compressing and resizing it to 700px. Squoosh is not just yet another alternative for TinyPNG, it outperforms it in quality and other configuration options.

unsplash minified

Other Alternative Tools

ImageMagick is also one of the most popular offline alternatives for image compression. Understanding all the options and getting a perfect configuration might be a challenge for people who are new to ImageMagick. It is a full-blown image manipulation application and compression is a small part of its overall capability.

Subscribe To Our Newsletter
Loading

Leave a comment