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?

2

1 Answer

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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