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:
- jpegoptim – is a utility to optimize/compress JPEG files without loosing quality.
- OptiPNG – is a small program that optimize PNG images to smaller size without losing any information.
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
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
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.
Does either of these optimization tools retain metadata embedded in the image? (such as Exif, etc)
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!
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`
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%.
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:
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%?
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!
Very nice! Does the batch operation work recursively?
+ 1 for recursive please?
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
how folder with sub folder too?
command find will process all subfolders automatically
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”)
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?
jpegoptim dir/**/*.jpg
when I run the command I get something like this:
Type the following command:
Sample outputs:—-
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.
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.