Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How can Automator or Applescript rotate 500,000 images in folders and subfolders.

Hi Everyone,

I have very specific question.

I've got 500,000 images that sit in 98 subfolders. Some of the subfolders contain 25,000 images.

Now my question: is there any script/automator workflow that I can use?

I was trying to create a workflow:

1. Ask for Finder Items

2. Get Folder Contents (tick repeat for each subfolder found)

3. Rotate images

It works OK, but only for these folders where there is less than 4,096 files:

I normally receive following error message:

'Rotate images failed - 1 error

too many arguments (12019) -- limit is 4096'

Is there any way to increase this limit or create completely different apple script?

I really hope that someone will help me with this one.

By the way my OS is Mountain Lion

Thank you

OS X Mountain Lion (10.8)

Posted on Dec 1, 2012 5:42 AM

Reply
31 replies

Dec 1, 2012 6:27 AM in response to ludwip

this should do what you want, but it modifies the original files directly so (needless to say) please test it on copies.


set folderToProcess to choose folder with prompt "Choose a folder to process"

set folderPath to quoted form of POSIX path of folderToProcess


-- 13 is a spotlight constant for images that I can't find documented anywhere (I got it by saving a spotlight image search in the Finder and examining it in a text editor). FYI.

set fileList to paragraphs of (do shell script "mdfind -onlyin " & folderPath & " '_kMDItemGroupId == 13'")


tell application "Image Events"

repeat with thisImageFile in fileList

set couldNotOpen to false

try


-- open the image file

set openedFile to openthisImageFile

on error


-- image file can't be opened for some reason. you could display an alert here, or write the file path to a log

set couldNotOpen to true

end try


if couldNotOpen is false then

tell openedFile


-- you didn't specify which way or how much you wanted to rotate. this rotates them 90° clockwise

rotateto angle 90


-- save back to same file

save

end tell

end if

end repeat

end tell

Dec 1, 2012 10:13 AM in response to twtwtw

Hi,

Thanks for the reply but it doesn't work for me I'm afraid.

I've run test by copying two folders one with 11,000 images another one with 400 images.

This is the reply that I've got straight after running script.


tell application "AppleScript Editor"


choose folder with prompt "Choose a folder to process"


--> alias "Mac OS:Users:xxxx:Desktop:Automation:"

end tell

tell current application


do shell script "mdfind -onlyin '/Users/xxxx/Desktop/Automation/' '_kMDItemGroupId == 13'"


--> ""

end tell


I would apreciate suggestions or am I doing something wrong?

Thank you

Dec 1, 2012 10:49 AM in response to ludwip

well, for mdfind to work it needs the folder to be indexed. it's possible that you just didn't wait long enough after duplicating for the indexing to complete, or that that particular folder is blocked from indexing (it shouldn't be, unless you've explicitly blocked spotlight from indexing your user account or desktop).


try this: open a Finder find window (⌘-F) for that folder and search for 'Kind is Image'. That's precisely the same as using the mdfind command. If nothing shows up in the Finder window, you need to index the folder; if files do show up, try the script again and see what happens.

Dec 1, 2012 4:14 PM in response to ludwip

Here is a Bash script that is run in the Terminal. It prompts for the explicit path to the main image folder. From there, it recursively finds specified image files in subfolders, and uses multi-processing to rotate these images in place by nn clockwise degrees. Tested this on a copy of my Pictures folder and every image was rotated correctly. All image characteristics were retained. You may want to try this on a copy of a much larger folder to ascertain its effectiveness.


There is verbose output from this script as it runs. If it encounters an error, it exits gracefully.


chmod +x rotate.sh

./rotate.sh



#!/bin/bash
#
# rotate.sh - Recursively finds all specified image files in
#                  sub-folders, then uses multi-processing to
#                  rotate images (in place) clockwise by specified
#                  degrees. Image characteristics are retained.
#
# Employs the sips(1) scriptable image processing system, also
# documented in Apple TN2035 at:
# http://developer.apple.com/library/mac/#technotes/tn2035/_index.html
#
# Example: rotate.sh <return>
#                Full path to start folder? /Users/login-name/BigImageFolder
#
usage()
{
    echo "$0 /the/filesystem/path/to/starting/folder"
    echo "Exiting ..."
    exit 1
}

command_error()
{
    echo "$_ has encountered an error"
    echo "Quitting ..."
    # flush and empty disk cache
    /usr/bin/purge
    exit 1
}


# if any application returns an error - quit gracefully
trap command_error ERR


read -p "Full path to start folder? " STARTFOLDER
# avoid empty response
[[ -z $STARTFOLDER ]] && usage

# Image file formats. Handles mixed or upper case
TIF="*.tif*"
JPG="*.jp*g"
GIF="*.gif"
PNG="*.png"

# images rotated clockwise by this amount
DEGREES=90

# Hunt for these image files
FINDIMG="-name $TIF -o -name $JPG -o -name $PNG -o -name $GIF"

# Run two parallel processes that rotate every subordinate image file
# Have dual-core, four-thread i5 CPU, so reserve two threads for the system
find $STARTFOLDER -type f \( $FINDIMG \) -print0 | xargs -0 -P 2 -I {} sips -r $DEGREES {} 

# no trapped ERR event, so normal completion
printf "\n%s\n" "Finished image Rotations."
  
# flush and empty disk cache
/usr/bin/purge
exit 0

Dec 2, 2012 3:27 AM in response to twtwtw

twtwtw,


Thanks again for your help.

However there is another error:


' error "Image Events got an error: Can’t get image \"00353391301.JPG\"." number -1728 from image "00353391301.JPG" '


It worked great with the first subfolder but got stuck with the second.


Is there anything that I've done wrong this time?


Thanks again

Dec 2, 2012 3:31 AM in response to VikingOSX

VikngOSX,

Thanks for your imput.

However there are two scipts in your post.

This one: '

chmod +x rotate.sh

./rotate.sh'

as well as former (longer one). Which one should I use?


Thanks



VikingOSX wrote:


Here is a Bash script that is run in the Terminal. It prompts for the explicit path to the main image folder. From there, it recursively finds specified image files in subfolders, and uses multi-processing to rotate these images in place by nn clockwise degrees. Tested this on a copy of my Pictures folder and every image was rotated correctly. All image characteristics were retained. You may want to try this on a copy of a much larger folder to ascertain its effectiveness.


There is verbose output from this script as it runs. If it encounters an error, it exits gracefully.


chmod +x rotate.sh

./rotate.sh



#!/bin/bash # # rotate.sh - Recursively finds all specified image files in #                  sub-folders, then uses multi-processing to #                  rotate images (in place) clockwise by specified #                  degrees. Image characteristics are retained. # # Employs the sips(1) scriptable image processing system, also # documented in Apple TN2035 at: # http://developer.apple.com/library/mac/#technotes/tn2035/_index.html # # Example: rotate.sh <return> #                Full path to start folder? /Users/login-name/BigImageFolder # usage() {     echo "$0 /the/filesystem/path/to/starting/folder"     echo "Exiting ..."     exit 1 } command_error() {     echo "$_ has encountered an error"     echo "Quitting ..."     # flush and empty disk cache     /usr/bin/purge     exit 1 } # if any application returns an error - quit gracefully trap command_error ERR read -p "Full path to start folder? " STARTFOLDER # avoid empty response [[ -z $STARTFOLDER ]] && usage # Image file formats. Handles mixed or upper case TIF="*.tif*" JPG="*.jp*g" GIF="*.gif" PNG="*.png" # images rotated clockwise by this amount DEGREES=90 # Hunt for these image files FINDIMG="-name $TIF -o -name $JPG -o -name $PNG -o -name $GIF" # Run two parallel processes that rotate every subordinate image file # Have dual-core, four-thread i5 CPU, so reserve two threads for the system find $STARTFOLDER -type f \( $FINDIMG \) -print0 | xargs -0 -P 2 -I {} sips -r $DEGREES {} # no trapped ERR event, so normal completion printf "\n%s\n" "Finished image Rotations."   # flush and empty disk cache /usr/bin/purge exit 0 

Dec 2, 2012 5:13 AM in response to ludwip

What do you mean by that?


set mainFolder to POSIX path of (path to desktop as text) & "My Folder"

This line points to your main folder if it is located on your Desktop and named "MyFolder". You can also use something like this:

set mainFolder to "/Users/ ludwip/Documents/My Folder"

A quick way of getting the path to your folder is to drag the folder itself into the AppleScript editor.


The second line issues the rotate commands to sips.

do shell script "find " & quoted form of mainFolder & " \\! -name \".*\" -type f -print0 | xargs -0 sips -r 90"


You can learn more by typing "man sips" or "man xargs" in terminal.

Dec 2, 2012 6:14 AM in response to ludwip

Thanks for your imput.

However there are two scipts in your post.

This one: '

chmod +x rotate.sh

./rotate.sh'

as well as former (longer one). Which one should I use?



There are two distinct commands to run in the terminal. You do both, on separate command lines as shown. The assumption is that you are currently in your home login directory. Again, I would advise running this on a few files, then stop, and analyze the files to ensure you are satisfied. Then apply it to a larger subset, and review. Then to all images you wish to rotate.


  1. chmod +x rotate.sh
    1. Use the chmod OS X command to change the script permissions to executable
    2. This does not run the script or alter contents.
  2. ./rotate.sh
    1. Start the rotate.sh script
    2. The script will prompt you for the directory path to the main image folder where you wish to begin
      1. If on an externally mounted drive, then /Volumes/drivename/path-to-image-folder
      2. This script does not use restricted system syntax to operate on your image files, as it expects that their access privileges are already owned by your login name.

Dec 2, 2012 7:13 AM in response to ludwip

well, there are a couple of possibilities here:


  1. system events is quitting (it will do that on its own after a few minutes of inactivity) and he script is getting confused when it has to restart
  2. the file 00353391301.JPG is not a valid image file (corrupt data, or an unsupported file format with the wrong extension, or the wrong type of file entirely, or something like that).


so, covering the bases, try this revision:


set folderToProcess to choose folder with prompt "Choose a folder to process"

set folderPath to quoted form of POSIX path of folderToProcess


-- 13 is a spotlight constant for images that I can't find documented anywhere (I got it by saving a spotlight image search in the Finder and examining it in a text editor). FYI.

set fileList to paragraphs of (do shell script "mdfind -onlyin " & folderPath & " '_kMDItemGroupId == 13'")


tell application "Image Events"


-- tell image event not to quit itself

set quit delay to 0


repeat with thisImageFile in fileList

try


-- open the image file

set openedFile to openthisImageFile


tell openedFile


-- you didn't specify which way or how much you wanted to rotate. this rotates them 90° clockwise

rotateto angle 90


-- save back to same file

save

end tell

on error errstr


-- image events had an error - dislpay alert, then skip this file

my displayError(thisImageFile, errstr)

end try

end repeat



-- reset Image Event's automatic quitting

set quit delay to 900

end tell


on displayError(thisImageFile, errstr)

tell application "System Events"

display alert "Image Events Error" message "file : " & thisImageFile & return & return & "error: " & errstr

end tell

set imageAlias to POSIX filethisImageFile

tellapplication "Finder"

set label index of item imageAlias to 1

end tell

end displayError


This makes three changes - it tells Image Events not to quit itself during script execution, it expands the error handling block to cover all the Image Events functions (this will report more detail and not interrupt further processing), and as a convenience labels any problematic file orange in the finder so it's easier to find.


Also, I should point out that adayzdone and ludwip are presenting orthogonal approaches (one that finds files using the unix 'find' utility rather than mdfind, and one that's entirely unix-based using the underlying sips utility). Treat each approach as its own thing; don't get confused and try to implement parts of each of them. People on this forum sometimes forget how confusing help can be when helpers are working off of different models and fail to thoroughly contextualize their posts.

Dec 2, 2012 10:52 AM in response to VikingOSX

Oooo, good point. It would probably be ok since it's reusing the same openedFile variable (that should automatically release the last image data), but to be on the safe side ludwip could replace the save line with the following:



closesavingyes

that would definitely avoid any such problems. He might even want to add the following lines to the on error section:


try

closeopenedFilesavingno

end try

which will try to close any opened image in the event of an error.

How can Automator or Applescript rotate 500,000 images in folders and subfolders.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.