Rotate images from terminal
Solution 1
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")" ;
done
Above 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)
Solution 2
for file in *.JPG; do convert $file -rotate 90 rotated-$file; done
This 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.
Solution 3
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 *.jpg
CAVEAT: This isn't a lossless rotation method for JPEG files, https://www.imagemagick.org/discourse-server/viewtopic.php?t=5899.
jpegtran
achieves this (untested):
# counterclockwise
ls *.jpg | xargs -n 1 jpegtran -perfect -rotate 270
# clockwise
ls *.jpg | xargs -n 1 jpegtran -perfect -rotate 90
Solution 4
Here's how I do it:
-
Install gThumb
sudo apt-get install gthumb
-
Open up nautilus and go to your images directory. Right click on one and choose
Open with -> gthumb
. -
Under the
view
menu chooseBrowser
or 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
Tools
and thenRotate Right
orRotate Left
depending on your preference.
Solution 5
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}; done
This 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.
Related videos on Youtube
Comments
-
pranphy over 1 year
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
andshotwell --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.
-
Rinzwind about 9 yearsaskubuntu.com/a/432662/15811 if you want it from nautilus. But the terminal command you want is "convert" from "imagemagick"
-
tomchiukc over 2 yearsand to have jpegtran installed, type: $ sudo apt-get install libjpeg-progs
-
-
George Udosen about 7 yearsPlease check your code and the link given!
-
Paul Jones almost 5 yearsWorks perfectly
-
EA304GT almost 5 yearsMogrify is quite a Swiss knife when it comes to simple, quick, batch image editing
-
mLstudent33 over 4 yearsbest answer, why wouldn't you want to do this in-place?
-
krlmlr over 4 years@mLstudent33: Good question. It appears that
mogrify
isn't lossless for JPEG files, butjpegtran
is. -
Geremia almost 4 yearsThis isn't lossless.
-
Fabby almost 4 years@Geremia That depends on the file format used. If it's a TIFF or BMP that you rotate and (keep the same destination file format) it will be perfectly lossless. If you use JPEG or PNG (both lossy file formats) the result will be lossy as well, but that was not what the original question was about... 0:-)
-
BenKoshy over 3 yearsthx for your answer. would you know if this is lossy for jpegs?
-
Paddy Landau over 3 yearsThis runs serious risk of data loss! You must use quotation marks, thus:
CUR_DIR="$PWD"
(note the simplification of using the built-in variable),cd "$1"
,convert "$file" -rotate 90 "$file"
andcd "$CUR_DIR"
-
Paddy Landau over 3 yearsYou can simplify further by using
pushd
andpopd
. They're great for changing directory and back again. -
Eric Duminil almost 3 yearsNote that the default jpg output of gthumb has really low quality. Rotations of jpg can be done losslessly with
jpegtran
. -
David V. about 2 yearsThat doesn't explain how to do it from the terminal.