0

I have a few thousand rectangular images of various sizes. I want them all to have a longer height than a width. I want to do this by rotating all images whose height is shorter than their width 90 degrees. How do I do this with imagemagick?

2

You can write a simple function like below, to getting the width &height of the image then check if w>h then rotate it.

rotate() {
    (( $(identify -format '%w > %h' "$1") )) && convert "$1" -rotate 90 "rotated_$1"; 
}

Call it like rotate image.png.


Another approach which is suggested and presented by user414777 using exif tool, that is doing the rotation based on the EXIF Orientation tags:

exif -co rotated_"$1" --ifd=0 --tag=0x0112 --set-value=6 "$1"

Switches explanations:

TagID    TagName       Writable  Group   Values/Notes
0x0112   Orientation   int16u    IFD0    1 = Horizontal (normal)
                                         2 = Mirror horizontal
                                         3 = Rotate 180
                                         4 = Mirror vertical
                                         5 = Mirror horizontal and rotate 270 CW
                                         6 = Rotate 90 CW
                                         7 = Mirror horizontal and rotate 90 CW
                                         8 = Rotate 270 CW
3
  • With jpeg files, just changing their exif orientation tag may be enough, which would be order of magnitude faster than physically rotating the image. Eg. exif -co new.jpg --ifd=0 --tag=0x0112 --set-value=6 old.jpg
    – user414777
    Oct 4 '20 at 4:28
  • And changing the IFS, creating an array, is a bit of overkill. With bash/ksh/zsh, you can do just un_landscape(){ (( $(identify -format '%w > %h' "$1") )) && convert "$1" -rotate 90 "rotated_$1"; }. This, of course, just in your answer, assuming they don't already have an exif orientation tag, in which case the width/height will have no relationship to the way in which they're displayed ;-)
    – user414777
    Oct 4 '20 at 4:41
  • good suggestion and golfed version, I will update in my answer later if you don't mind. thanks Oct 4 '20 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.