1. Introduction

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.

2. Using ImageMagick

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.

2.1. Using convert

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:

  • find . starts a search in the current directory and its subdirectories
  • -maxdepth 1 restricts the search to only the current directory (no subdirectories)
  • -type f filters the results to include only files
  • -name “*.jpg” specifies that we’re looking for files with the .jpg extension
  • exec convert {} -rotate 90 {} \; for each matching file found, it runs the convert command to rotate the image by 90 degrees clockwise

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.

2.2. Using mogrify

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.

3. Using jpegtran

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.

3.1. Rotate One Image

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.

3.2. Rotate Multiple Images

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:

  • for img in *.jpg; do initiates a for loop that iterates through all JPEG files in the current directory
  • -rotate 90 specifies the rotation of 90 degrees clockwise
  • -copy all tells jpegtran to copy all markers from the original image to the rotated image
  • -outfile “rotated-$img” specifies the name of the output file for the rotated image
  • “$img” represents the current input image filename in each iteration of the loop

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.

4. Using ffmpeg

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:

  • -i input.jpg specifies the input image file
  • output.jpg specifies the output image file
  • -vf “transpose=1” transposes the image, effectively rotating it 90 degrees counter-clockwise

Here, we can adjust the value in the transpose filter to achieve different rotations:

  • transpose=1 rotates 90 degrees counter-clockwise
  • transpose=2 rotates 90 degrees clockwise
  • transpose=3 rotates 180 degrees
  • transpose=0 or omitting the filter: No rotation

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.

5. Using exiftool

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:

  • Orientation = 1 no rotation
  • Orientation = 2 flipped horizontally
  • Orientation = 3 rotates 180 degrees
  • Orientation = 4 rotates 180 degrees and then flips horizontally
  • Orientation = 5 rotates 90 degrees counter-clockwise and then flips horizontally
  • Orientation = 6 rotates 90 degrees clockwise
  • Orientation = 7 rotates 90 degrees clockwise and then flips horizontally
  • Orientation = 8 rotates 90 degrees counter-clockwise

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.

6. Conclusion

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.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.