1. Home
  2. Linux

How To Resize Image Files On Linux

Image files can be exceptionally large. Even if you’re dealing with JPEGs, the file sizes can and do exceed 2 and even 5 MB in size. If you have a RAW file, it’s going to be bigger. If you need to keep a large library of images on your Linux PC, you’ll need to learn how to save space. One of the best ways to save space with files is by resizing them. If you have a large photo library, you can resize image files and shrink your photo library’s size.

Resizing image files is a tricky thing as too much of it can really ruin the quality. That’s why in this guide we’ll go over how to resize image files the right way.

Install ImageMagick

The Linux operating system has a lot of quirky, unique tools. One such tool is Convert. It’s a simple little app bundled inside of ImageMagick that can manipulate image files through the command line. To get access to the Convert tool, you’ll need to install ImageMagick. Most Linux distributions have this installed, though if yours doesn’t, you can install it easily enough.

To install the ImageMagick package, open up a terminal window and enter the following command listed under your OS.

Ubuntu

sudo apt install imagemagick

Debian

sudo apt-get install imagemagick

Arch Linux

sudo pacman -S imagemagick

Fedora

sudo dnf install imagemagick

OpenSUSE

sudo zypper install imagemagick

Other Linuxes

As stated earlier, ImageMagick is a critical component of how a lot of programs display and manipulate images in programs on Linux. That being said, your distribution may be using an alternative. Look in your operating system’s package manager for “ImageMagick” and install it. Once installed, you’ll have access to Convert.

Compress Images With Convert

Compressing images can often reduce their quality. For good looking, smaller image files it’s best to re-size them. Going this route can help keep the overall quality of the image while keeping the file size much smaller.

To resize with Convert, open up a terminal window, find an image file you’d like to manipulate and then use the CD command to move the terminal to its location. In this example, picture files will be in /home/username/pictures/.

cd ~/Pictures/

Use the convert command to resize. Try to resize the image by about 20%, as this gives you a good balance of quality and file size. If you need to go lower than 20%, try 25%, 30%, or 40%. Keep in mind that quality of the image decreases the more it is resized.

convert -resize 20% image-file-name-original.jpg image-file-name-resized.jpg

Convert works with different file types, aside from the JPG used in the example. To resize, modify this command with the correct file extension and new size. Be sure that you specify both the original file name and an output filename.

Compress Multiple Images

Convert is excellent at manipulating and compressing one image at a time, but it’s tedious to compress images one by one. the good news is that Convert can be manipulated with bash to parse and work with multiple image files at once.

Note: batch compressing image files with multiple file names likely won’t work. Only batch convert files with the same file type.

Batch converting is easy, and it starts out by creating a folder to work in. Having a folder for all of these image files you plan to convert is good, otherwise, dozens of image files will litter your file system. Using the mkdir command, create a working directory.

mkdir -p ~/Pictures/Convert-Images/

Then, open the file manager app, find the newly created folder and move all of the image files you plan to convert to this folder. After moving the files, use the CD command in a terminal to move into the new folder as well.

cd ~/Pictures/Convert-Images/

In the terminal, type out this command. It will tell Convert to resize multiple files at once, creating output files with “resize” at the end. In the code, the command will look for JPG images. If you’re working with PNG files or another format supported by the Convert app, change *.jpg to *.png, etc.

for img in *.jpg; do
  convert -resize 20% "$img" "opt-$img"
done

Resizing Script

The batch resizing command is nice, as it works very well. However, having to type out a long command and tweak it every time can be annoying. To shorten the work, consider making it into a script. Open up a terminal window, and use the touch command to create a new file. This file will hold the code for our conversion script.

touch ~/Pictures/Convert-Images/batch-resize.sh

Next, open up the Nano text editor.

nano ~/Pictures/Convert-Images/batch-resize.sh

Paste the following code inside of the script file:

#!/bin/bash

# Catch user input for file type.

echo "Enter the file extension for your image files:"

# Store user input in $files.

read files

# Resize images.

for img in *.$files; do
convert -resize 20% "$img" "resize-$img"
done

Save the resize script in Nano by pressing Ctrl + O. Close the editor with Ctrl + X.

Update the permissions of the script so it runs. Don’t skip this part, or the script won’t work correctly!

chmod +x ~/Pictures/Convert-Images/batch-resize.sh

To use the script, place all image files you’d like to convert in ~/Pictures/Convert-Images/. Then CD in and execute the script. When entering the file extension (like JPG, PNG and etc) don’t use a period, or the script will break!

cd ~/Pictures/Convert-Images/
./batch-resize.sh

Leave a comment