Linux: Jpeg Image Optimization / Compress Command

I know how to optimize and compress png images using optipng command. line tool. I have lots of images in JPEG format. How do I compress and optimize JPEG images on my amazon cloud account so that I can save bandwidth on cloudfront cdn account? How do I use an image compressor to create lossless compression on JPEG files, with no effect on image quality in bulk using Linux?

JPG file format is recommended for high resolution photographic-style images. You need to use jpegoptim command. It is used to optimize/compress jpeg files. Program supports lossless optimization, which is based on optimizing the Huffman tables. And so called “lossy” optimization where in addition to optimizing Huffman tables user can specify upperlimit for image quality.


Type the following command:
$ apt-get install jpegoptim
Sample outputs:

[sudo] password for vivek: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libavutil-extra-51 libggiwmh0-target-x libggi2 libgii1 libvo-aacenc0
  libgii1-target-x mplayer-skin-blue libggiwmh0 libggi-target-x
Use 'apt-get autoremove' to remove them.
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 5 not upgraded.
Need to get 14.0 kB of archives.
After this operation, 77.8 kB of additional disk space will be used.
Get:1 squeeze/main jpegoptim amd64 1.2.3-2+b1 [14.0 kB]
Fetched 14.0 kB in 1s (11.2 kB/s)    
Selecting previously deselected package jpegoptim.
(Reading database ... 333683 files and directories currently installed.)
Unpacking jpegoptim (from .../jpegoptim_1.2.3-2+b1_amd64.deb) ...
Processing triggers for man-db ...
Setting up jpegoptim (1.2.3-2+b1) ...


The syntax is:

jpegoptim file.jpeg
jpegoptim [options] file.jpeg

Type the following command to optimize photo.jpeg, enter:
$ jpegoptim photo.jpeg
Sample outputs:

photo.jpeg 1312x948 24bit JFIF  [OK] 25226 --> 10744 bytes (57.41%), optimized.

How do I process files in batch?

Use the bash for loop as follows:

for i in one.jpeg two.jpeg foo.jpeg; do jpegoptim "$i"; done


 ## process all *.jpeg in the current directory 
for i in *.jpeg; do jpegoptim "$i"; done


From the man page:

      -d, --dest=
             Sets  alternative  destination  directory  where  to save optimized files (default is to overwrite the originals). Please note that unchanged
             files won't be added to the destination directory. This means if the source file can't be compressed, no file will be created in the destina‐
             tion path.

       -f, --force
             Force optimization, even if the result would be larger than the original file.

       -h, --help
             Displays short usage information and exits.

       -m[0..100], --max=[0..100]
             Sets  the maximum image quality factor (disables lossless optimization mode, which is by default enabled). This option will reduce quality of
             those source files that were saved using higher quality setting.  While files that already have lower  quality  setting  will  be  compressed
             using the lossless optimization method.

       -n, --noaction
             Don't really optimize files, just print results.

       -o, --overwrite
             Overwrite target file even if it exists (when using -d option).

       -p, --preserve
             Preserve file modification times.
       -q, --quiet
             Quiet mode.

       -t, --totals
             Print totals after processing all files.

       -v, --verbose
             Enables verbose mode (positively chatty).

             Strip all (Comment & Exif) markers from output file. (NOTE! by default only Comment & Exif markers are kept, everything else is discarded)

             Strip Comment (COM) markers from output file.

             Strip EXIF markers from output file.

             Strip IPTC markers from output file.

             Strip ICC profiles from output file.

  1. jpegoptim home page.
  2. See man page – jpegoptim(1)

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.