ScaleScaleScaleScale

Tips / Nginx


Optimizing JPG / PNG images on Linux

There are lot of online tools and apps to optimize your images, however the problem comes when you have lot of images to process, then those tools are not so well designed and we have to search for more practical ways to do it. If you have come to this point… then you must considering using Image Optimization tools from the Linux command line.

Optimizing JPG and JPEG images on Linux

In order to optimize jpeg and jpg images we will use a tool called: jpegoptim, that is available in the normal Linux repos for Debian, Ubuntu and also for CentOS / RHEL if you use EPEL repo.

Installing jpegoptim on Debian/Ubuntu

apt-get install jpegoptim

Install jpegoptim on CentOS / RHEL

Install EPEL repo and run:

yum install jpegoptime

How to use jpegoptim command?

jpegoptim file.jpg

Simply replace “file.jpg” with your real file name, you will see an output like this:

[root@server.myserver.com:~]jpegoptim ei.jpg
ei.jpg 80x15 24bit N Adobe JFIF  [OK] 2340 --> 2325 bytes (0.64%), optimized.
[root@server.myserver.com:~]

At the output we will see how much % was the image optimized, in case your image is already optimized the percentaje will be less than usual, as in this example, or you will see a “skipped” message, to let you know that there was no need to optimize the image.

Mass Image Optimization

You can use wildcards in a bash loop, example:

for i in /home/usrame/images/*.jpg; do jpegoptim $i; done

Or a file name list, example:

for i in me.jpg you.jpg he.jpg; do jpegoptim $i; done

Optimizing PNG images

For PNG image optimization we will use pngcrush tool.

Install pngcrush Ubuntu / Debian:

apt-get install pngcrush

Other Linux distros compile from source at http://pmt.sourceforge.net/pngcrush/:

wget http://downloads.sourceforge.net/project/pmt/pngcrush/1.7.78/pngcrush-1.7.78.tar.gz
tar -xvpzf pngcrush-1.7.78.tar.gz
cd pngcrush-1.7.78
make
cp pngcrush /usr/bin

Use the “-brute” option to get the best compression, this are some of the mos common options:

-brute (use brute-force: try 126 different methods [11-136])
-c color_type of output file [0, 2, 4, or 6]
-d directory_name (where output files will go)

Example:

pngcrush -brute -e "myimage_compressed.png" myimage.png

Mass png optimization:

pngcrush -brute -d "/home/user/images" *.png

Do you know any other image optimization tools?

Popular search terms:

  • optimize PNG Linux
  • debian jpegcrush
  • https://www scalescale com/tips/nginx/optimizing-jpg-png-images-linux/
  • linux server optimize images
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • I used mogrify but is too slow.
    I found this in EPEL jpegoptim not jpegoptime,
    thank you.