Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
In the dynamic realm of digital media, the ability to manipulate and transform images is an essential skill. Linux, renowned for its versatility, offers a variety of potent tools within the terminal for these tasks.
In this tutorial, we’ll explore various ways to rotate images in a Linux terminal.
ImageMagick is a software suite for image manipulation in Linux. Hence, it provides a collection of command-line utilities and programming libraries that enable users to create, edit, convert, and manipulate images.
First, let’s install imagemagick via apt-get and sudo:
$ sudo apt-get install imagemagick
Once installed, we can use the following commands.
convert is a powerful tool for image manipulation and conversion. Hence, it can rotate, resize, crop, and perform other operations on images.
Let’s see how we can use convert to rotate images:
$ convert input.jpg -rotate 90 output.jpg
In this case, convert takes the input.jpg image, rotates it by 90 degrees clockwise, and saves the result as output.jpg in the current working directory. Moreover, the original image is overwritten with the rotated version if we don’t specify an output filename.
Of course, we can rotate all images in the current directory:
$ find . -maxdepth 1 -type f -name "*.jpg" -exec convert {} -rotate 90 {} \;
In this example, we see a more complex command to work on multiple files:
In summary, we use find to locate all JPEG files in the current directory. Then we use the convert command to rotate each JPEG file by 90 degrees clockwise, overwriting the original files with the rotated versions.
mogrify is a command-line tool commonly used for batch processing and manipulation of image files in Linux. Moreover, we can use mogrify to apply image transformations, such as resizing, rotating, or converting formats, to multiple images at once.
Let’s rotate images via mogrify:
$ mogrify -rotate -90 *.jpg
Here, we rotate all JPEG images in the current directory by 90 degrees counter-clockwise, and the original files are overwritten with the rotated versions.
jpegtran is a command-line utility for transforming and manipulating JPEG image files in Linux. In fact, it enables us to perform operations like rotation, cropping, and flipping without degrading image quality because it preserves the original JPEG compression.
jpegtran is part of the libjpeg-progs package. Let’s install the package first:
$ sudo apt-get install libjpeg-progs
Then, we can rotate one image or multiple images.
Let’s see how we use jpegtran to rotate one image:
$ jpegtran -rotate 90 -outfile output.jpg input.jpg
In this case, jpegtran takes the input.jpg image, rotates it by 90 degrees clockwise, and saves the rotated version as output.jpg in the current working directory. In fact, the original file remains unchanged.
We use a for loop with jpegtran to rotate multiple images:
$ for img in *.jpg; do
jpegtran -rotate 90 -copy all -outfile "rotated-$img" "$img"
done
Now, let’s explore each part of the script above:
In summary, we rotate all JPEG images in the current directory, and the rotated versions are saved with filenames prefixed by rotated-. Moreover, the original images remain unchanged.
ffmpeg is primarily a multimedia framework commonly used for tasks related to audio and video processing. While it’s not designed for image manipulation, it can be used to rotate images by treating them as videos with a single frame.
First, let’s install ffmpeg:
$ sudo apt-get install ffmpeg
Then, we can use ffmpeg to rotate images:
$ ffmpeg -i input.jpg -vf "transpose=1" output.jpg
Now, let’s break down this rotation:
Here, we can adjust the value in the transpose filter to achieve different rotations:
Besides, we can rotate an image in ffmpeg using the rotate filter:
$ ffmpeg -i input.jpg -vf "rotate=90*PI/180" output.jpg
In this instance, 90*PI/180 converts 90 degrees to radians, so the image is rotated by 90 degrees counter-clockwise.
exiftool is a powerful command-line tool for reading, writing, and manipulating metadata in image files and other types of files. While it’s primarily used for metadata operations, it can also perform image rotation based on EXIF information stored in the image files.
First, let’s install exiftool:
$ sudo apt-get install exiftool
Then, we can rotate images:
$ exiftool -Orientation=6 -n sample.jpg
In this case, exiftool sets the Orientation tag in sample.jpg to 6 which indicates a 90-degree clockwise rotation. Moreover, we used -n to display the raw, machine-readable values for the metadata.
Notably, this operation effectively rotates the image 90 degrees clockwise in the EXIF metadata. This means that can affect how the image is displayed in some image viewers that respect the orientation tag. However, this command doesn’t physically rotate the image data; it only updates the metadata. This means it’s up to the reading software to interpret this metadata and apply it.
Here are some common Orientation options:
Alternatively, we can specify the orientation in another way:
$ exiftool -Orientation='Rotate 90 CW' sample.jpg
Here, the value Rotate 90 CW isn’t the standard numeric value used in EXIF metadata but is a human-readable description of the desired orientation.
In this article, we’ve explored various methods to rotate images in a Linux terminal. Each method offers distinct capabilities, catering to diverse image manipulation needs. Hence, Linux provides a range of options for adjusting image orientations according to our needs.