How to Resize and Optimize Images From the Linux Terminal

It is faster to edit your images using your keyboard.

How to Resize Images From the Linux Terminal Featured Image

If you are a Linux user and prefer the Terminal to any other graphical applications, then you will be happy to know that you can also resize, convert, and optimize your images directly in the Terminal with ImageMagick. ImageMagick is a suite of tools for Linux which allows you to manipulate images from the command line. It’s also the image processor behind many graphics-related applications. Here we will show you how to resize your images from the Terminal.

Installing ImageMagick

Before we start, we need to first install ImageMagick on our system.

On Ubuntu or any Debian-based distro:

sudo apt install imagemagick

On Fedora:

sudo dnf install ImageMagick

On Arch Linux

sudo pacman -S imagemagick

View the Image Information

With ImageMagick installed, you can now use it to view all the information about an image.:

identify -verbose <image_name>

For example, to check the original dimension of the image, use the following command:

identify -format "%wx%h" <image_name>
Command To Check The Dimension Of An Image.

For a photo, you can also view the Exif data:

identify -verbose <image_name> | grep exif

Resizing Images

Now, let’s try to resize it to 600X300px. The command is:

convert <image_name> -resize 600x300 <new_image_name>
Command To Resize And Save A New Image

You can also resize the image to a specific percentage. For example, to scale down “Maketecheasier.png” by 50%, we will use the following command:

convert <image_name> -resize 50% <new_image_name>

The resizing method here is not restricted to scaling down only. You can also use it to upscale an image. For example, to double the size of an image:

convert <image_name> -resize 200% <new_image_name>

If you want to overwrite the original image, you can use the mogrify command instead. It is similar to the convert command, but it is easier to use and it overwrites the original file.

mogrify -resize 400x200 <image_name>

Batch Resize All Images of a Folder

You can easily resize all images in a particular directory. It lets you bulk resize images to specific dimensions. For instance, there are five images in the “Screenshots” directory, and we want to resize the size of these images by 50%. Therefore, we will run the following commands:

cd ~/Screenshots
for img in *.jpg; do
convert -resize 50% "$img" "Resized_$img"; done

Note: Please change the image format from the above command if you are dealing with any other image format rather than JPG.

Commands To Resize Images In Bulk

Change the image format

If you want to change the format of an image, you just have to change the output name to the new format. For example, to convert “Maketecheasier.png” to “Maketecheasier.jpg”, use the following command:

convert Maketecheasier.png Maketecheasier.jpg

Reduce Image Quality

You can also change the quality of images with ImageMagick. For example, when you take a screenshot on your system, it’s generally captured as a png file in a large size. To reduce the size, you can reduce the screenshot’s quality and convert it into a jpg file:

convert screenshot.png -quality 80 screenshot-new.jpg

Rotate Images

To rotate images, all you have to do is use the -rotate option along with the number of degrees you want the image to rotate. For example, the following command creates a 90-degree rotated “screenshot.jpg:”

convert screenshot.jpg -rotate 90 screenshot.jpg

Create GIF file

If you have a bunch of jpg files and you want to create an animated GIF, ImageMagick can do it too. All you have to do is to load all the jpg files to convert and output it as gif:

cd Screenshots
convert *.jpg animated.gif

Add Watermark to Images

Sometimes you may need to add some text or a logo/graphic to an existing image. For this you use the –append option. In this example, I’m adding a bar to the bottom of my image with a note about the contents.

tux_penguin
convert tux.jpg -background Khaki label:'This is Tux!' -gravity Center -append tux_annotated.jpg
imagemagick-annotated

Remove Exif Data From Image

As a privacy measure, it is sometimes useful to remove all the Exif data from a JPEG photo. You can do it with the mogrify command:

mogrify -strip IMG_0123.JPG

Frequently Asked Questions

Is ImageMagick still maintained?

Yes, ImageMagick has been available since the 1990s and is still maintained regularly. ImageMagick’s new stable update was released on June 20th, 2022 and this suite of tool has active update policies to improve it with the latest releases.

Can I resize images without losing quality?

No, because resizing the image will always reduce its quality. If you reduce the size of an image from 350px to 250px, the picture will be less clear when resized back to its original size.

Is there a graphical application for ImageMagick?

ImageMagick is mainly a command line tool and the developer didn’t create a GUI tool for it. However there are many third-party software that use ImageMagick as its backend. One example is FotoKilof.

Is ImageMagick secure?

Initially, there were some security-related issues in ImageMagick, but now it is safe to use. ImageMagick has a firm security policy to make it safer for a user. The developers also removed bugs that were causing privacy issues.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox