I have a large set of JPEG pictures all with the same resolution. It would take too long to open each one inside the graphical interface of imagemagic or gimp.
How do I achieve each picture being rotated and saved as the same filename?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use the convert command:
convert input.jpg -rotate <angle in degrees> out.jpg
To rotate 90 degrees clockwise:
convert input.jpg -rotate 90 out.jpg
To save the file with the same name:
convert file.jpg -rotate 90 file.jpg
To rotate all files:
for photo in *.jpg ; do convert $photo -rotate 90 $photo ; done
Alternatively, you can also use the mogrify command line tools (the best tool) recommended by @don-crissti:
mogrify -rotate 90 *.jpg
Method 2
For JPEG images and right-angle rotations, use jpegtran or exiftran, as they can rotate the images losslessly.
for f in *.jpg ; do
jpegtran -rotate 180 "$f" > "${f%.jpg}-rotated.jpg"
done
Or to rotate in-place:
for f in *.jpg ; do
jpegtran -rotate 180 -outfile "$f" "$f"
done
exiftran also has the -a flag to automatically rotate the image based on what the EXIF orientation tag says.
Method 3
Note that the two other answers may provide different results depending on the EXIF Orientation : it seems that convert rotates with regards to the EXIF Orientation, while jpegtran just ignores the EXIF Orientation.
This observation led me to figure I actually needed to discard the EXIF Orientation, so I just used exiftool to discard EXIF data without further data loss (that’s also what does jpegtran when no -rotate option is given, it seems) :
exiftool -all= -o outfile.jpg infile.jpg
I could have just removed the EXIF Orientation with
exiftool -Orientation= -o outfile.jpg infile.jpg
or modified it with
exiftool -n -Orientation=1 -o outfile.jpg infile.jpg
(for this later case you will need to read the FAQ to understand option -n, needed for exiftool to translate the -Orientation value, and the EXIF tags table).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0