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?
21 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:
The basis for this code snippet is the
forloop which finds alljpgfiles in a certain directory:for i in *.jpg do <Insert command here....> doneBut some exclusions have to be made before a command is run and for this we nest a conditional statement using
if...The second structure is an
ifstatement which importantly contains thetestfor 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:
The appropriate test, which searches for jpgs that are of a required size:
[ $(identify -format "%[fx:w]x%[fx:h]\n" "$i") = 1920x1080 ]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 :)