If you want to resize, convert or modify images on a Linux system, there are a few terminal commands you can use. Here’s how:
- Open a terminal window and type the following command:
convert -resize
x image.jpg - To resize an image without changing its aspect ratio, use the following command:
convert -resize
x image_unsized.jpg ..
ImageMagick is a suite of command-line utilities for modifying and working with images. ImageMagick can quickly perform operations on an image from a terminal, perform batch processing of many images, or be integrated into a bash script.
ImageMagick can perform a wide variety of operations. This guide will introduce you to ImageMagick’s syntax and basic operations and show you how to combine operations and perform batch processing of many images.
Installation
ImageMagick isn’t included in the default installations of Ubuntu and many other Linux distributions. To install it on Ubuntu, use the following command:
Converting Between Formats
The convert command takes an image, performs actions on it, and saves the image with the file name you specify. One of the most basic things you can do with it is converting images between formats. The following command takes a PNG file named “howtogeek.png” in the current directory and creates a JPEG image from it:
You can also specify a compression level for JPEG images:
The number must be between 1 and 100. ImageMagick uses the quality level of the input image, if possible. If not, ImageMagick defaults to 92.
Resizing Images
The convert command can also quickly resize an image. The following command asks ImageMagick to resize an image to 200 pixels in width and 100 pixels in height:
We’ve used the same file name here, so ImageMagick will overwrite the original file.
ImageMagick will try to preserve the aspect ratio if you use this command. It will alter the image to fit within a 200×100 area, but the image may not be exactly 200×100. If you want to force the image to become a specific size — even if it messes up the aspect ratio — add an exclamation point to the dimensions:
You can also specify a specific width or height and ImageMagick will resize the image to that width or height while preserving the aspect ratio. The following command will resize an image to a width of 200:
The following command will resize an image to a height of 100:
Rotating an Image
ImageMagick can quickly rotate an image. The following command takes an image named howtogeek.jpg, rotates it by 90 degrees and saves the rotated image as howtogeek-rotated.jpg:
If you specified the same file name, ImageMagick would save the rotated image over the original image file.
Applying Effects
ImageMagick can apply a variety of effects to an image. For example, the following command applies the “charcoal” effect to an image:
The charcoal command applies an artistic “charcoal” style effect to an image — the 2 in the command lets you control the strength of the effect.
The following command applies the “Implode” effect with a strength of 1:
The implode effect makes it appear as if there’s a black hole at the center of the image.
Combining Operations
All these operations can be combined. With a single command, you could resize an image, rotate it, apply an effect, and convert it to another format:
This is just the start of what you can do with ImageMagick. There are many more operations you can combine.
Batch Processing
You can take advantage of Bash to quickly do batch processing of many images. For example, the following command would take all PNG files in the current directory, rotate them, and save a new copy of each with “rotated-” added to the beginning of each file name.
You can easily modify this command to perform other actions. You can also integrate batch processing commands into a Bash shell script to automate image-processing operations.
Any article on ImageMagick will omit a lot of what you can do with it — there are just too many options and commands. If you’re interested in doing more with ImageMagick, check out the official documentation on the ImageMagick website for a much more in-depth look at ImageMagick.