How to Optimize and Compress JPEG or PNG Images in Linux Commandline

You have a lot of images, and want to optimize and compress the images without losing its original quality before uploading them to any cloud or local storages? There are plenty of GUI applications available which will help you to optimize the images. However, here are two simple command line utilities to optimize images and they are:

  1. jpegoptim – is a utility to optimize/compress JPEG files without loosing quality.
  2. OptiPNG – is a small program that optimize PNG images to smaller size without losing any information.
Compress and Optimize Images in Linux
Compress and Optimize JPEG and PNG Images in Linux

Using these two tools, you can either optimize a single or multiple images at a time.

Compress or Optimize JPEG Images from Command Line

jpegoptim is a command line tool that can be used to optimize and compress JPEG, JPG and JFIF files without losing its actual quality. This tool supports lossless optimization, which is based on optimizing the Huffman tables.

Install jpegoptim in Linux

To install jpegoptim on your Linux systems, run the following command from your terminal.

On Debian and it’s Derivatives
# apt-get install jpegoptim
or
$ sudo apt-get install jpegoptim
On RedHat based Systems

On RPM based systems like RHEL, CentOS, Fedora etc., you need to install and enable EPEL repository or alternatively, you can install the epel repository directly from the commandline as shown:

# yum install epel-release
# dnf install epel-release    [On Fedora 22+ versions]

Next install jpegoptim program from the repository as shown:

# yum install jpegoptim
# dnf install jpegoptim    [On Fedora 22+ versions]

How to Use Jpegoptim Image Optimizer

The syntax of jpegoptm is:

$ jpegoptim filename.jpeg
$ jpegoptim [options] filename.jpeg

Let’s now compress the following tecmint.jpeg image, but before optimizing the image, first find out the actual size of the image using du command as shown.

$ du -sh tecmint.jpeg 

6.2M	tecmint.jpeg

Here the actual file size is 6.2MB, now compress this file by running:

$ jpegoptim tecmint.jpeg 
Optimize JPEG Image in Linux
Optimize JPEG Image in Linux

Open the compressed image in any image viewer application, you will not find any major differences. The source and compressed images will have the same quality.

The above command optimizes the images to the maximum possible size. However, you can compress the given image to a specific size to, but it disables the lossless optimization.

For example, let us compress above the image from 5.6MB to around 250k.

$ jpegoptim --size=250k tecmint.jpeg
Optimize Image Fix Size
Optimize Image Fix Size

Batch JPEG Image Compression and Optimization

You might ask how to compress the images in the entire directory, that’s not difficult too. Go to the directory where you have the images.

tecmint@tecmint ~ $ cd img/
tecmint@tecmint ~/img $ ls -l
total 65184
-rwxr----- 1 tecmint tecmint 6680532 Jan 19 12:21 DSC_0310.JPG
-rwxr----- 1 tecmint tecmint 6846248 Jan 19 12:21 DSC_0311.JPG
-rwxr----- 1 tecmint tecmint 7174430 Jan 19 12:21 DSC_0312.JPG
-rwxr----- 1 tecmint tecmint 6514309 Jan 19 12:21 DSC_0313.JPG
-rwxr----- 1 tecmint tecmint 6755589 Jan 19 12:21 DSC_0314.JPG
-rwxr----- 1 tecmint tecmint 6789763 Jan 19 12:21 DSC_0315.JPG
-rwxr----- 1 tecmint tecmint 6958387 Jan 19 12:21 DSC_0316.JPG
-rwxr----- 1 tecmint tecmint 6463855 Jan 19 12:21 DSC_0317.JPG
-rwxr----- 1 tecmint tecmint 6614855 Jan 19 12:21 DSC_0318.JPG
-rwxr----- 1 tecmint tecmint 5931738 Jan 19 12:21 DSC_0319.JPG

And then run the following command to compress all images at once.

tecmint@tecmint ~/img $ jpegoptim *.JPG
DSC_0310.JPG 6000x4000 24bit N Exif  [OK] 6680532 --> 5987094 bytes (10.38%), optimized.
DSC_0311.JPG 6000x4000 24bit N Exif  [OK] 6846248 --> 6167842 bytes (9.91%), optimized.
DSC_0312.JPG 6000x4000 24bit N Exif  [OK] 7174430 --> 6536500 bytes (8.89%), optimized.
DSC_0313.JPG 6000x4000 24bit N Exif  [OK] 6514309 --> 5909840 bytes (9.28%), optimized.
DSC_0314.JPG 6000x4000 24bit N Exif  [OK] 6755589 --> 6144165 bytes (9.05%), optimized.
DSC_0315.JPG 6000x4000 24bit N Exif  [OK] 6789763 --> 6090645 bytes (10.30%), optimized.
DSC_0316.JPG 6000x4000 24bit N Exif  [OK] 6958387 --> 6354320 bytes (8.68%), optimized.
DSC_0317.JPG 6000x4000 24bit N Exif  [OK] 6463855 --> 5909298 bytes (8.58%), optimized.
DSC_0318.JPG 6000x4000 24bit N Exif  [OK] 6614855 --> 6016006 bytes (9.05%), optimized.
DSC_0319.JPG 6000x4000 24bit N Exif  [OK] 5931738 --> 5337023 bytes (10.03%), optimized.

You can also compress multiple selected images at once:

$ jpegoptim DSC_0310.JPG DSC_0311.JPG DSC_0312.JPG 
DSC_0310.JPG 6000x4000 24bit N Exif  [OK] 6680532 --> 5987094 bytes (10.38%), optimized.
DSC_0311.JPG 6000x4000 24bit N Exif  [OK] 6846248 --> 6167842 bytes (9.91%), optimized.
DSC_0312.JPG 6000x4000 24bit N Exif  [OK] 7174430 --> 6536500 bytes (8.89%), optimized.

For more details about jpegoptim tool, check out the man pages.

$ man jpegoptim 

Compress or Optimize PNG Images from Command Line

OptiPNG is a command line tool used to optimize and compress PNG (portable network graphics) files without losing its original quality.

The installation and usage of OptiPNG is very similar to jpegoptim.

Install OptiPNG in Linux

To install OptiPNG on your Linux systems, run the following command from your terminal.

On Debian and it’s Derivatives
# apt-get install optipng
or
$ sudo apt-get install optipng
On RedHat based Systems
# yum install optipng
# dnf install optipng    [On Fedora 22+ versions]

Note: You must have epel repository enabled on your RHEL/CentOS based systems to install optipng program.

How to Use OptiPNG Image Optimizer

The general syntax of optipng is:

$ optipng filename.png
$ optipng [options] filename.png

Let us compress the tecmint.png image, but before optimizing, first check the actual size of the image as shown:

tecmint@tecmint ~/img $ ls -lh tecmint.png 
-rw------- 1 tecmint tecmint 350K Jan 19 12:54 tecmint.png

Here the actual file size of above image is 350K, now compress this file by running:

tecmint@tecmint ~/img $ optipng tecmint.png 
OptiPNG 0.6.4: Advanced PNG optimizer.
Copyright (C) 2001-2010 Cosmin Truta.

** Processing: tecmint.png
1493x914 pixels, 4x8 bits/pixel, RGB+alpha
Reducing image to 3x8 bits/pixel, RGB
Input IDAT size = 357525 bytes
Input file size = 358098 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0		IDAT size = 249211
                               
Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 0		IDAT size = 249211

Output IDAT size = 249211 bytes (108314 bytes decrease)
Output file size = 249268 bytes (108830 bytes = 30.39% decrease)

As you see in the above output, the size of the tecmint.png file has been reduced up to 30.39%. Now verify the file size again using:

tecmint@tecmint ~/img $ ls -lh tecmint.png 
-rw-r--r-- 1 tecmint tecmint 244K Jan 19 12:56 tecmint.png

Open the compressed image in any image viewer application, you will not find any major differences between the original and compressed files. The source and compressed images will have the same quality.

Batch PNG Image Compression and Optimization

To compress batch or multiple PNG images at once, just go the directory where all images resides and run the following command to compress.

tecmint@tecmint ~ $ cd img/
tecmint@tecmint ~/img $ optipng *.png

OptiPNG 0.6.4: Advanced PNG optimizer.
Copyright (C) 2001-2010 Cosmin Truta.

** Processing: Debian-8.png
720x345 pixels, 3x8 bits/pixel, RGB
Input IDAT size = 95151 bytes
Input file size = 95429 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 0		IDAT size = 81388
                               
Selecting parameters:
  zc = 9  zm = 8  zs = 0  f = 0		IDAT size = 81388

Output IDAT size = 81388 bytes (13763 bytes decrease)
Output file size = 81642 bytes (13787 bytes = 14.45% decrease)

** Processing: Fedora-22.png
720x345 pixels, 4x8 bits/pixel, RGB+alpha
Reducing image to 3x8 bits/pixel, RGB
Input IDAT size = 259678 bytes
Input file size = 260053 bytes

Trying:
  zc = 9  zm = 8  zs = 0  f = 5		IDAT size = 222479
  zc = 9  zm = 8  zs = 1  f = 5		IDAT size = 220311
  zc = 1  zm = 8  zs = 2  f = 5		IDAT size = 216744
                               
Selecting parameters:
  zc = 1  zm = 8  zs = 2  f = 5		IDAT size = 216744

Output IDAT size = 216744 bytes (42934 bytes decrease)
Output file size = 217035 bytes (43018 bytes = 16.54% decrease)
....

For more details about optipng check man pages.

$ man optipng

Conclusion

If you’re a webmaster and wants to serve optimized images over your website or a blog, these tools can be very handy. These tools not only saves the disk space, but also the reduces the bandwidth while uploading the images.

If you know any other better way to achieve same thing, do let us know via comments and don’t forget to share this article on your social networks and support us.

Senthil Kumar
A Linux Consultant, living in India. He loves very much to write about Linux, Open Source, Computers and Internet. Apart from that, He'd like to review Internet tools and web services.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

36 thoughts on “How to Optimize and Compress JPEG or PNG Images in Linux Commandline”

  1. Hey there! jpegoptim does not work with filenames that start with – (dash). It considers that you pass an argument and It fails. Does anybody knows an workaround for this situation? Thank you!

    Reply
    • Wow. jpegoptim really fails to accept "--" (double-dash) as ending sequence for parameters. With many binaries you can: `jpegoptim — -myfile.jpg` – But this fails silently.

      So Plan B could be: `jpegoptim –stdin –stdout > -output.jpg < -input.jpg`

      Reply
  2. This is a very nifty little app, but sadly I find the compressions to be minimal to say the least. On average with most of my images, it would only reduce the file size by about 3% whereas when i use something like ffmpeg or imagemagick I am able to compress those same files down by about 80%.

    Reply
    • I actually experienced the opposite, optipng does great, if you try to use ffmpeg the file gets 10x larger.

      Take a file, for example, image7.png and do the following:

      # optipng -o7 -zm1-9 -strip all image7.png -out image7-optipng.png
      # ffmpeg -i image7.png image7-ffmpeg.png
      # convert image7.png -define png:compression-level=9 image7-magik.png
      # pngcrush image7.png image7-crush.png
      
      Reply
  3. Hello, may I ask?

    How to specify the image compression level in png format

    For example, 1-100. What should I do if I want to press the picture to 60%?

    Reply
  4. Copying your commands didn’t work for me/Arch Linux. Changing *.JPG to *.jpg did.

    However it only reduced the size about 3%, so I used jpegoptim --size=750k *.jpg.

    The pics still look great on the screen. Thank you very much!

    Reply
      • You can use the find command to recursively find all .jpeg and .jpg files and then pass them to jpegoptim, that should do it for you.

        The command will look like this

        # Jpegoptim
        find images_folder/ *.{jpeg,jpg} -exec jpegoptim {} \;

        # OptiPNG
        find images_folder/ *.png -exec optipng {} \;

        Replace `images_folder` with the path for whatever directory you have your files in

        Reply
        • Hi,

          this usage of `find` in conjunction with `jpegoptim` (and possibly also with `optipng`) will call the `jpegoptim` command for every file `find` matches.

          In case anyone is looking for processing all matching files in a single run, try the following:

          `find path/to/images/ -regex ".*\.\(jpg\|jpeg\)" -exec jpegoptim {} +`

          Note the `+` at the end of the `exec` command, which leads to `exec` finally being called with all matching files.

          Source: https://www.booleanworld.com/guide-linux-find-command/ (right above “Executing commands on directories”)

          Reply
        • Hi, how are you? Thanks so much for this. Made a bash script to run on my site’s image folder and it works well. Working on creating a cronjob for automation. Now I have a question. How can I do the same with GIFs, WEBP, and SVGs?

          Reply
  5. 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
      libvo-amrwbenc0
    Use 'apt-get autoremove' to remove them.
    The following NEW packages will be installed:
      jpegoptim
    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 http://mirror.anl.gov/debian/ 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) ...
    
    Reply
  6. Finally, I have been looking for such tools for image optimization on Linux. Until now, i have always required intenet for image optimizations. Thanks for the post.

    Reply
  7. Hello, just wanted to thank you for the article. I needed a way to optimize my images to pass google’s pagerank and this helped immensely. Once again thanks for the easy to read tutorial.

    Reply
  8. This tutorial helped me a lot, one of sites I have to optimize it’s images for more faster loading, result now: https://gtmetrix.com/reports/aswakbladi.com/013Tow1p

    It is better the doing it manually. For me I set those options for JPG images:

    # jpegoptim -m 70 -s *.jpg *.jpeg *.JPG *.JPEG
    

    -s option will strip other information reside inside image, and also it will save you extra bandwidth and results in faster loading.

    PNG images:

    # optipng -o4 *.png
    
    Reply
    • @Vule,

      Size doesn’t matter, you can optimize any size images from 10KB to 10MB, what error you getting while opening optimized image? which tool you used to view images? do you tried with different image viewers?

      Reply
    • @Giorgos,

      Thanks for the tip about Trimage tool, but we’re getting 404 error while accessing the page, seems no more development and also I found that the last update was 5 years ago on Github

      Reply
  9. I use ‘convert’
    With a small script take all the files from ./orig and create thumbnails in ./pics and a larger picture in ./pics-big
    it also creates the entries for the html file
    it reduces the image to 28% and 64% of their original dimensions – but that is how my camera is set up

    #!/bin/bash
    cd orig;
    for file in *; do convert -strip -scale 64%x64% -quality 75 $file “../pics-big/”$file;
    convert -strip -scale 28%x28% -quality 75 $file “../pics/”$file;
    echo ” ” >> ../html.txt;
    done;

    Reply
    • @Yavor,

      Thanks a ton for sharing the script with us, I was looking for such script since long time for my camera images, which are huge in size..let me try and get back to you..

      Reply
  10. with OptiPNG i was familiar with, so after reading your article, I gave a try to jpegoptim: it was useless for my final JPEGs, which are produced with GIMP, and are destined to web use – 0.00% improvement. on a batch of JPEGs taken straight from the camera, for the batch I tested, the average space savings was 2.36%
    my conclusion: it would be useless for a webmaster trying to minimize bandwidth use, it wasn’t able to do a thing.

    Reply
    • @Nicu,

      I totally agree with your point that jpegoptim not a best tool for JPEG or JPG compression, it just reduce the size around 5-10% that’s totally useless to use, whereas OptinPNG works great it optimize images nearly 30-40% but still I can say it’s not a good choice to use for websites, where speed is more important. Even I tried different online jpeg or png compression tools, still not a good results specially for jpeg images…

      Reply

Leave a Reply to Aamnah Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.