How to Compress and Optimize Your Images

To compress your images from the Linux command line, you’ll need two programs — one for *.jpg file types and one for *.png file types.

  • jpegoptim (for *.jpg file types)
  • optipng (for *.png file types)

Compress JPG

To install jpegoptim:

$ apt-get install jpegoptim

To compress all *.jpg files in a directory:

$ jpegoptim -m 50 *.jpg

The command-line switch “-m 50” disabled lossless compression.

-m<quality>, –max=<quality>
set maximum image quality factor (disables lossless optimization mode, which is by default on)
valid quality values: 0 – 100

If you’d prefer to maintain lossless, remove this switch.

Compress PNG

To install optipng:

$ apt-get install optipng

To compress all *.png files in a directory:

$ optipng *.png

Source(s): TecMint