How to Manipulate Images in the Linux Terminal

Ever tire of constantly opening up your favorite image editor for a simple crop, resize, or to change the file format? Maybe you have a need to easily perform these tasks in batch or within software?

Here’s how to use the Linux convert tool, which allows you to do all this with terminal via the command line, and much more.

Install ImageMagick

The convert tool is part of the popular imagemagick package, which you need to have installed. Check to see whether or not ImageMagick is installed with the command:

convert --version

If the version number of ImageMagick is displayed, then it’s already installed and you may move onto the next section. Otherwise, install ImageMagick with the command:

sudo apt-get -y install imagemagick

Get Image Information

You may get the basic information on any image with the command:

convert  -print "Type: %m
Size: %b
Dimensions: %wx%h
" /dev/null

This will give you the image mime type, filesize in KB, and its dimensions in pixels, and will look similar to:

Type: jpeg
Size: 35.6KB
Dimensions: 640 x 480

Change Image File Format

You may easily change the file format of any image with:

convert  -append 

This command will take the file, convert it into the proper format and save a new image at You must use the correct file extensions (i.e., JPG, GIF, PNG, etc.) for the DEST_FILE and it will automatically detect which format to convert the image to.

Resize an Image in Linux

If you want to resize an image, or maybe quickly generate a thumbnail, you can do so with the -resize option. Run the following command:

convert -resize 25%  

This will resize the image, and create a new image file at DEST_FILE that is 25% the dimensions of the source image. Please note, the above command does not reduce the size by 25%, but instead generates a new image that is 25% of the size. If you need to enlarge an image, you may adjust the percent to greater than 100%.

Instead of resizing by percentage, you may also specify the specific dimensions with the command:

convert -resize 1024x768 profile.jpg profile_large2.jpg

The above command would resize the source image to 1024×768 pixels, and save the new image at DEST_FILE.

Crop an Image

If you need to take a little off the top or sides, you may crop an image with the following command:

convert  -crop 640x250+0+0 

For example, if the source image was 640×480 pixels, the above command would result in DEST_FILE being an image of 640×250 pixels containing the top half of the image while the bottom half was cropped out. The +0+0 in the above command specifies the top left most point of the image, or in other words, where to begin cropping.

Using the above example, you may save the bottom half of the image instead with the command:

convert  -crop 640x250+0+230 

This will begin the cropping at 0x230 within the source image, cropping out the top half and leaving only the bottom half.

Rotate Image

You can even rotate images by using the command:

convert  -rotate 90 

The above command will rotate the source image by 90 degrees clockwise and save the resulting image in DEST_FILE.

Add Caption to Image

Another excellent feature of the convert tool is the ability to instantly add captions to images, including where in the image to place the caption, plus the font family, size and color to use. Use the following command to add a caption to an image:

convert profile.jpg -pointsize 14 -font helvetica -fill yellow -draw 'text 270,160 "Warm Regards from the Family" ' profile_cap.jpg

The above command will place a caption of “Warm Regards from the Family” onto the source image using the font Helvetica 14pt in yellow, with the top left point of the image starting at 270×160 pixels, and save the results in DEST_FILE.

Please note, you must be careful with the -draw option, which must begin in single quotes followed by the location of the top left most point, then by the text of the caption surrounded by double quotes.

There is a huge list of colors available, and if desired you may list all colors supported by your computer with the command:

cb:>showrgb

The selection of fonts varies depending on your computer, but all standard fonts that are generally used will be there. Each operating system is a little different, but in Ubuntu for example, the fonts are located within the /usr/share/fonts directory and contains a list of all available system fonts you may use in the above command.

Mix and Match Image Editing Commands

You may mix and match any of the above options into a single command for greater efficiency. For example, if you have a large image that is in the wrong orientation, 2571×1200 pixels in size, and you wanted to generate a 300×200 thumbnail you could use the command:

convert  -rotate 90 -crop 1200x800 -resize 25% 

The above would first rotate the image by 90 degrees, making the new size 1200×2571 pixels. Next it would crop the excess off the bottom so it’s only 800 pixels in height, and finally resize the image to 25% of its original size resulting in a final thumbnail 300×200 pixels in size.

Batch Process an Image Directory

Have an entire directory of images you want to process with the same command? It’s no problem in Linux. For example, you might wish to generate thumbnails of all images in your current working directory by resizing them to 30% of their current:

mkdir thumbs
for i in `ls | grep .jpg$`; do convert $i -resize 30% thumbs/$i; done

The above command will go through all files in your current directory that have a .jpg extension, resize them to 30% of their size, and place the resulting thumbnails in the /thumbs/ sub-directory.

Image Manipulation Made Easy!

Through this tutorial you’ve hopefully taken away the ability to painlessly and effortlessly manipulate images via the terminal, and easily batch process an entire directory with a single command.

You’ve learned what ImageMagick’s convert tool is, plus how to change file format, resize, crop, rotate, and add captions to images with ease. There is also a great deal of additional functionality available—try the convert -help option for a full list.

Source: makeuseof.com

Related posts

How to Clear Your Browser History on Chrome, Firefox, Brave, and More

Connections #328: Today’s Answer and Clues (Saturday, May 4, 2024)

These 5 iPhone Features Helped Me Minimize Distractions at Work