Optimize images from cmdline

image_editor_compThere are many situations where you would want to optimize or compress images. For example, to send family pics to your parents or to upload in your website. Though new image formats are emerging, JPEG, PNG and GIF are still the most used formats. This article explores 3 different tools to optimize each of these formats. The methods are lossy but the parameters are tunable for least visual difference. Though some of these are available in the default Ubuntu repos, we will get the latest versions and compile those from source (on Ubuntu).

1. jpegoptim

Excellent tool to optimize JPG or JPEG files. Many web services use this in the background.


$ sudo apt-get install libjpeg-dev
$ git clone https://github.com/tjko/jpegoptim
$ cd jpegoptim
$ ./configure
$ make
$ sudo make install


$ jpegoptim --strip-all -m65 ~/myimage.jpg -d ~/tmp

–strip-all : strip all (Comment & Exif) markers from output file
-m[0..100] : set maximum image quality factor (disables lossless optimization mode, which is on by default)
-d  : specify alternative destination directory for optimized files (default behaviour is to overwrite original)


$ ll -tr ~/myimage.jpg ~/tmp/myimage.jpg
-rw-r--r-- 1 neo neo 929K Jan 2 12:19 /home/neo/myimage.jpg
-rw-rw-r-- 1 neo neo 251K Apr 8 07:40 /home/neo/tmp/myimage.jpg

2. pngquant

Optimizes PNG files (often up to 70%) and preserves full alpha transparency.


$ sudo apt-get install libpng-dev
$ git clone git://github.com/pornel/pngquant
$ cd pngquant
$ ./configure
$ make
$ sudo make install


$ pngquant --quality=65-80 myimage.png -o myimage_comp.png

–quality min-max : don’t save below min, use fewer colours below max (0-100)
-o : output file name


$ ll -tr ~/myimage*
-rw-r----- 1 neo neo 2.2M Jan 5 00:56 /home/neo/myimage.png
-rw-rw-r-- 1 neo neo 675K Apr 8 08:04 /home/neo/myimage_comp.png

3. gifsicle

Optimizes GIF files in addition to creating, editing, and getting information about GIF images and animations.


$ git clone https://github.com/kohler/gifsicle
$ cd gifsicle
$ autoreconf -i
$ ./configure
$ make
$ sudo make install


$ gifsicle --colors 256 -O2 myimage.gif -o myimage_comp.gif

–colors N : reduce the number of colours to N (omit if you want to retain full colour space)
–optimize[=LEVEL] : optimization level


$ ll -tr mygif*
-rw-r----- 1 neo neo 251K Apr 8 06:41 mygif.gif
-rw-rw-r-- 1 neo neo 53K Apr 8 08:16 mygif_comp.gif

3 thoughts on “Optimize images from cmdline”


Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s