Compressing Images On The Linux Command Line
sudo apt-get install jpegoptim optipng (Debian/Ubuntu) or sudo yum install jpegoptim optipng (CentOS/RHEL), then run jpegoptim image.jpg or optipng image.png. For WebP conversion, use cwebp image.jpg -o image.webp. These tools typically reduce file size by 20–40% with no visible loss in quality.Introduction
If you are looking to optimize and compress your images without compromising the original quality, before uploading them to local storage or cloud, command line tools can help you with this. Here are two commonly used tools:
- jpegoptim: This specialized utility performs optimization and compression of JPEG files while maintaining original quality. The JPEG file achieves optimization through the adjustment of its Huffman coding tables.
- OptiPNG: This stands as a specialized utility for PNG image optimization that achieves file size reduction while maintaining complete data integrity. It applies numerous compression techniques alongside delta filters and strives to minimize file size to its smallest potential.
This blog will guide you on how to install these two tools to compress pictures in Linux by using the command line.
Installation of jpegoptim and OptiPNG
For CentoOS or RPM based Linux Distribution
sudo yum install epel-release sudo yum install jpegoptim optipng
For Debian and other APT-based Linux distributions
sudo apt-get install jpegoptim optipng
How to Use jpegoptim to compress JPEG files
1. Use the below command to compress only a single file .jpg
In this example, the original JPEG (.jpg) files are stored as ~/jpeg.
cd ~/jpeg jpegoptim example.jpg
2. Use the below command to compress all the file .jpg files
cd ~/jpeg jpegoptim *.jpg
Original files will be overwritten and replaced with the optimized and compressed version.
3. To keep the original files, specify a directory to save the compressed version by using the below command:
cd ~/jpeg mkdir optim jpegoptim *.jpg -d ~/jpeg/optim
4. To find more information about how to use the jpegoptim command, use the -h flag to get their help file:
jpegoptim -h
How to use OptiPNG to Compress and Optimize PNG, BMP, GIF, PNM, and TIFF files
Here are a some examples on how to use OptiPNG to add lossless compression to images
1. To compress a single file .png, use the below command:
cd ~/pic optipng example.png
2. To optimize a .bmp file example.bmp:
cd ~/pic optipng example.bmp
A file with a name example.png will be created in a source directory while the original example.bmp remains.
3. To compress all .png files in the source directory:
cd ~/pic optipng *.png
Original files will be overwritten and replaced with the compressed version.
4. To keep the original files safe, use the -keep flag below:
cd ~/pic optipng -keep *.png
Original files will stay as it is and suffixed with a .bak. For instance example.png.bak.
5. To keep the original files, specify the target directory to save the compressed version. Use the below command:
cd ~/pic optipng -dir ~/pic/optim *.png
Original files will remain the same in the source directory and the compressed version will be saved in the target directory.
6. To find more information about how to use the OptiPNG command, use the -h flag to view its help file:
optipng -h
Conclusion
Both jpegoptim and OptiPNG make it easy to compress images on Linux directly from the terminal without sacrificing quality – jpegoptim for JPG/JPEG files, OptiPNG for PNG, BMP, GIF, PNM, and TIFF. For larger workloads or automated pipelines, running these tools on a dedicated Linux server ensures faster processing and consistent performance.
FAQs
How do I compress a JPG file on Linux?
Use jpegoptim: jpegoptim example.jpg. This preserves visual quality while reducing file size through Huffman table optimization. To compress all JPGs in a folder, run jpegoptim *.jpg.
How do I compress a PNG file on Linux?
Use OptiPNG: optipng example.png. It applies lossless compression techniques and delta filters to shrink the file without losing any image data.
How do I compress JPG images on Ubuntu specifically?
Ubuntu uses APT, so install jpegoptim with sudo apt-get install jpegoptim, then run jpegoptim example.jpg or jpegoptim *.jpg for a whole folder. The same command works across all Debian-based distributions.
What’s the best way to reduce image size on Linux without losing quality?
Use jpegoptim for JPEGs and OptiPNG for PNGs — both are lossless-focused tools built specifically for size reduction with no visible quality loss, unlike generic resizing which can degrade images.
Is there a command-line image compressor for Linux that works on multiple formats?
Yes. jpegoptim handles JPEG, OptiPNG handles PNG, BMP, GIF, PNM, and TIFF, and cwebp converts images to WebP for even smaller file sizes — together they cover most common image formats from one terminal.