3

I would like to rotate all images in a directory that are of a specific geometry. Can I use ImageMagick for this? If so could you help me with the code for the command prompt?

  • Are these images all of the same type: gif, jpg, png? And what degree of rotation are you interested in? – andrew.46 Aug 31 '16 at 1:12
  • They are all jpg and I would like 90 degrees thank you :) – Maynard Aug 31 '16 at 1:41
2

Something like the following should serve your needs:

for i in *.jpg
do
if [ $(identify -format "%[fx:w]x%[fx:h]\n" "$i") = 1920x1080 ]
then
convert "$i" -rotate 90 "${i%.jpg}_rotated.jpg"
fi
done

Simply alter the 1920x1080 setting for your desired size and then copy and paste the whole snippet into a Terminal window in the directory containing your image files...

Explanation of the syntax...

First there is the structure:

  1. The basis for this code snippet is the for loop which finds all jpg files in a certain directory:

    for i in *.jpg
    do
    <Insert command here....>
    done
    

    But some exclusions have to be made before a command is run and for this we nest a conditional statement using if...

  2. The second structure is an if statement which importantly contains the test for the required jpg size:

    if <Insert test here...>
    then
    <Insert command here...>
    fi
    

Next the for loop and the if statement are nested, and added to the mix is:

  1. The appropriate test, which searches for jpgs that are of a required size:

    [ $(identify -format "%[fx:w]x%[fx:h]\n" "$i") = 1920x1080 ]
    
  2. The appropriate command, which rotates these jpg files by 90 degrees:

    convert "$i" -rotate 90 "${i%.jpg}_rotated.jpg"
    

And then the Bash magic works :)

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.