I have a directory with a lots of images but they are in the wrong orientation. I want to rotate the images to correct the orientation (mostly ±90o). Using image (shotwell photo) viewer I can rotate them individually by clicking the rotate button but that's too tedious.
I looked at man shotwell and shotwell --help-all but there's nothing that explains how to invoke the rotate command from the command line.
Is there any way I can invoke the rotate command of shotwell (or any other viewer) from the terminal? Or any other methods to rotate images are welcome too.
38 Answers
If you're looking for a pure bash implementation, ImageMagick's convert command is what you're looking for:
for szFile in /path/*.png
do convert "$szFile" -rotate 90 /tmp/"$(basename "$szFile")" ;
doneAbove will leave existing files intact and copy the newly rotated ones to /tmp so you can move or copy them somewhere else or even replace the existing ones after the conversion and after verification.
(and it'll work on all recent releases of Ubuntu as it's standard software)
2for file in *.JPG; do convert $file -rotate 90 rotated-$file; doneThis will copy-rotate-and-rename your files.
If you want to leave the original files untouched just yet, this method may work well for you...
Note that this is case-sensitive: if your files are named *.jpg replace with lower-case (or *.png ...) accordingly.
If you want to overwrite in-place, mogrify from the ImageMagick suite seems to be the easiest way to achieve this:
# counterclockwise:
mogrify -rotate -90 *.jpg
# clockwise:
mogrify -rotate 90 *.jpgCAVEAT: This isn't a lossless rotation method for JPEG files, .jpegtran achieves this (untested):
# counterclockwise
ls *.jpg | xargs -n 1 jpegtran -perfect -rotate 270
# clockwise
ls *.jpg | xargs -n 1 jpegtran -perfect -rotate 90 4 Here's how I do it:
Install gThumb
sudo apt-get install gthumbOpen up nautilus and go to your images directory. Right click on one and choose
Open with -> gthumb.Under the
viewmenu chooseBrowseror press the Esc key. This will open the directory browser showing all your images.Press Ctrl and select the images you want to rotate or select all of them via Ctrl + A.
On the toolbar, select
Toolsand thenRotate RightorRotate Leftdepending on your preference.
As krlmlr notes in their answer, jpegtran (installed on Ubuntu with sudo apt install libjpeg-turbo-progs) is available for lossless rotation, but because it outputs to standard output, the command to use it would be something like this:
$ for img in *.jpg; do jpegtran -rotate 90 -copy all $img > rotated-${img}; doneThis command will take each file in the current directory, and produce a rotated version (90° clockwise) copy of each file with the new name rotated-image-name.jpg. The switch -copy all is to copy all metadata to the new file.
For more information see man jpegtran.
An additional piece of information given that the original question referenced Shotwell: according to Shotwell's documentation, Shotwell's rotations and other edits will not actually edit the files on disk:
Shotwell is a non-destructive photo editor. It does not modify your original photographs. That is to say, if you crop a photo or adjust its colors, the photo file on disc remains untouched. Shotwell stores your edits in a database and applies them on the fly as necessary. This means you can undo any alterations you make to a photograph.
A nice solution is to make a set of backups (e.g. prefixed backup-), rotate the original files producing a set of new files (prefixed e.g. rotated-), giving you a set of
img-1.pngbackup-img-1.pngrotated-img-1.pngimg-2.png- ...and so on
The mv/cp tools [bash globbing] can only add prefixes, it's messy to take them away (it'd use parameter expansion, ewww...)
The rename tool allows you to use s/before/after/ substitution syntax (from the sed tool) to swap that safeguard prefix and overwrite the original files, so your overall process for a given set of pictures img-{1..n}.png would be:
for imgf in img-*.png; do mv "$imgf" "backup-$imgf"; done
for imgf in backup-img-*.png; do convert "$imgf" -rotate 90 "rotated-$imgf"; doneNote:
- you could use
cprather thanmv, but then there's 2 copies of the original lying around (downside is you get concatenation of prefixes, "rotated-backup-...") - rotation is clockwise (
-rotate 270gets you 90° anti-clockwise) - to track progress, add
echo "Rotating ${imgf#backup-} ... ";afterconvertcalls (beforedone) for a more compact form (e.g. a set of numbered files), use some parameter expansion like
echo "$(echo ${imgf#backup-img-} | cut -d\. -f 1)...";instead( You can't remove prefix and suffix in the same bash param expansion hence use
cut)
Then after verification you've not messed up, delete the pictures by moving them back to the original
rename 's/^rotated-//;' rotated-*
rm backup-img-* 1 You can copy/paste this code, and save it as rotate.sh
#!/bin/bash -e
CUR_DIR=$(pwd)
cd $1
for file in *.jpg
do convert $file -rotate 90 $file
done
cd $CUR_DIRAfter saving this file, run it from terminal using ./rotate.sh folder_containing_images.
How about exiftran - transform digital camera jpeg images
I know it's a bit limiting (jpeg) but it has automatic (using exif orientation tag).
for photo in *.jpg do exiftrans -ia $photo
doneAbout Exiftran, it is a command line utility to transform digital camera jpeg images. It can do lossless rotations like jpegtran(1), but unlike jpegtran(1) it cares about the EXIF data: It can rotate images automatically by checking the exif orientation tag; it updates the exif informations if needed (image dimension, orientation); it also rotates the exif thumbnail. It can process multiple images at once