Optimize images from the Linux Terminal

by time news

2023-11-03 13:29:59

Efficient Image Optimization in Linux: jpegoptim and optipng

When you run a blog or use online services, image optimization is essential. Fortunately, in the Linux world, there are powerful tools like jpegoptim and optipng that allow you to reduce the size of your images without sacrificing quality. This article will guide you through the process of editing and resizing images using these programs.

Optimizing JPEG Images with jpegoptim

jpegoptim is a versatile tool that allows you to optimize images in JPEG format. Its basic use simply involves the name of the file you want to optimize. Please note that this action will overwrite the original file.

If what you are looking for is Edit, resize, resize images from the Linux consolefollow this article.

jpegoptim archivo.jpg

If you prefer to save the optimized image in a different directory to avoid overwriting the original, you can use the “-d” option followed by the destination path.

jpegoptim -d ~/blog archivo.jpg

If you want to optimize multiple JPEG files at the same time, you can use wildcards to span all files in a directory. Go to the directory of interest and run the following command:

jpegoptim *.jpg

jpegoptim’s default compression algorithm is quite conservative, meaning it compresses the image with minimal quality loss. However, if you are willing to reduce the image quality to save more space, you can do so by using the “-m” parameter and setting a value between 0 and 100. For example, for 70% compression:

jpegoptim -m70 archivo.jpg

You can simulate compression before running it with the “-n” and “-t” options. This will show you how much space you will save without compromising image quality.

jpegoptim -n -t -m70 archivo.jpg

If you want the image to be a specific size, such as 100 KB, you can achieve this by using the “-S” option:

jpegoptim -S 100k archivo.jpg

Optimizing PNG Images with optipng

optipng is a tool similar to jpegoptim but is designed specifically for PNG format images. You can use it easily by providing the name of the PNG file you want to optimize:

optipng file.png

This process will reduce the file size without losing image quality. optipng can also be used to optimize multiple PNG files at the same time:

optipng image1.png image2.png image3.png

If you want to optimize all PNG files in a directory, you can use wildcards:

optipng *.png

It is even possible to perform optimization recursively on directories and subdirectories using the “find” tool:

find -type f -name “*.png” -exec optipng {} ;
o
find -type f -name “*.jpg” -exec jpegoptim {} ;
o
find -type f -name “*.jpg” -exec jpegoptim -m90 {} ;

Installing jpegoptim and optipng

Both tools, jpegoptim and optipng, are found in the repositories of the main GNU/Linux distributions. To install them, use the software manager corresponding to your distribution. For example, on Arch Linux-based systems like Antergos, you can use the following command:

sudo pacman -S jpegoptim optipng

# In Debian, Ubuntu and similar…

apt-get install optipng

# On RedHat, Fedora, AlmaLinux and similar

yum -y install optipng

Replace the above command with “yum -y install” on Fedora/CentOS and RedHat, or “apt-get install” on distributions like Debian, Ubuntu, and similar.

These tools provide an efficient way to optimize images on Linux, which can be especially valuable if you manage a website or blog and are looking to reduce bandwidth consumption and improve the loading speed of your pages. With jpegoptim and optipng, you can maintain the quality of your images while reducing their size. Optimize your images and optimize your site!

#Optimize #images #Linux #Terminal

You may also like

Leave a Comment