Linux provides many features to create, edit and share images. Sometimes you may need to simply rotate & resize images in Linux terminal. You can easily do this using ImageMagick library in Linux. In this article, we will learn how to rotate & resize images in Linux Terminal.
How to Rotate & Resize Images in Linux Terminal
Here are the steps to rotate & resize images in Linux terminal.
1. Install ImageMagick
Open terminal and run the following command to install ImageMagick.
$ sudo apt install Imagemagick # Ubuntu/Debian systems # yum install ImageMagick # RHEL/CentOS/Fedora
2. Rotate Image in Linux
There are several ways to rotate images in Linux using ImageMagick. The simplest way to do this is using convert function. Let us say you want to rotate file image.jpg by 90 degrees (counterclockwise).
$ convert /home/ubuntu/image.jpg -rotate 90 /tmp/image.jpg
The above command will rotate image.jpg and save a copy in /tmp/image.jpg. You can use the above convert command on almost all image formats.
If you want to rotate an image clockwise, use -90 instead of 90 in the above command.
$ convert /home/ubuntu/image.jpg -rotate -90 /tmp/image.jpg
If you want to convert all jpg files in /home/ubuntu, run a loop through these files and call convert command on each file separately.
for szFile in /home/ubuntu/*.jpg do convert "$szFile" -rotate 90 /tmp/"$(basename "$szFile")" ; done
This way you can automate image rotation of one or more images, by embedding the above code in your shell script.
Alternatively, you can also use mogrify the same way as convert, except that it will be a lossy conversion.
# counterclockwise: mogrify -rotate -90 /home/ubuntu/image.jpg # rotate 1 file mogrify -rotate -90 /home/ubuntu/*.jpg # rotate all files # clockwise: mogrify -rotate 90 /home/ubuntu/image.jpg mogrify -rotate 90 /home/ubuntu/*.jpg # rotate all files
The main difference between convert and mogrify commands is that mogrify rotates an image in place, that is, the original image will be replaced with new image in file. With convert, you can save it as a different file.
3. Resize Images in Linux
You can also use convert and mogrify commands to resize images, using -resize option. Here is the syntax to resize images using convert and mogrify commands.
$ convert source_file -resize dimensions destination_file $ mogrify -resize dimensions source_file
Let us say you want to resize file /home/ubuntu/image.png to 200px x 100px then here is the command to do it using convert and mogrify.
$ convert /home/ubuntu/image.png -resize 200x100 /home/ubuntu/new-image.png $ mogrify -resize 200x100 /home/ubuntu/image.png
In the above commands, the convert and mogrify commands will retain the original aspect ratio while resizing images. If you don’t want to retain aspect ratio, add bang(!) at the end of image dimensions.
$ convert /home/ubuntu/image.png -resize 200x100! /home/ubuntu/new-image.png $ mogrify -resize 200x100! /home/ubuntu/image.png
If you want to resize multiple images using convert command, run a for loop through them and run convert command on each of them. Here is an example to go through all .jpg files in /home/ubuntu and resize them to 200×100.
for szFile in /home/ubuntu/*.jpg do convert "$szFile" -resize 200x100 /tmp/"$(basename "$szFile")" ; done
If you are fine with in-place editing of images, you can use mogrify command. Here is the command to use mogrify to resize all jpg images in /home/ubuntu to 200px x 100px.
# counterclockwise: mogrify -resize 200x100 /home/ubuntu/*.jpg # rotate all files # clockwise: mogrify -resize 200x100 /home/ubuntu/*.jpg # rotate all files
Conclusion
In this article, we have learnt how to resize and rotate images using ImageMagick library’s convert and mogrify commands. We have also learnt how to resize & rotate one or more images. You can even combine image rotation and resizing a single command, by using both options together, as shown in the following commands.
#resize image to 200x100 and rotate 90 degrees using mogrify mogrify -rotate 90 -resize 200x100 /home/ubuntu/image.jpg #resize image to 200x100 and rotate 90 degrees using convert mogrify /home/ubuntu/image.jpg -rotate 90 -resize 200x100 /tmp/image.jpg
You can customize the above commands as per your requirement. There are many customizations available for image rotation and resizing. What we have shown is just a sample of all available options.
Also read:
How to Create Fillable PDF Forms in Linux
How to Limit Network Bandwidth in Linux
How to Exclude Files & Folders From Copying in Linux
How to Copy Files to CD in Linux
How to Merge Folders & Subdirectories in Python
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.