Skip navigation

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

1799 Views 31 Replies Latest reply: Dec 4, 2012 4:58 PM by adayzdone RSS
1 2 3 Previous Next
ludwip Level 1 Level 1 (0 points)
Currently Being Moderated
Dec 1, 2012 5:42 AM

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)
  • twtwtw Level 5 Level 5 (4,580 points)

    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 open thisImageFile

                        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

                                            rotate to angle 90

      -- save back to same file

                                            save

                                  end tell

                        end if

              end repeat

    end tell

  • twtwtw Level 5 Level 5 (4,580 points)

    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.

  • adayzdone Level 2 Level 2 (150 points)

    Try:

     

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

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

  • VikingOSX Level 5 Level 5 (4,700 points)

    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
    
  • etresoft Level 7 Level 7 (23,900 points)

    sips? I've never heard of that.

     

    <types "man sips">

    <now does web search for "sips image tool">

     

    It's days like this I love Apple Support Communities! Thanks!

  • twtwtw Level 5 Level 5 (4,580 points)

    the Image Events app is an applescript front end for sips; Apple's good at covering the bases.

  • adayzdone Level 2 Level 2 (150 points)

    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.

  • VikingOSX Level 5 Level 5 (4,700 points)

    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.
  • twtwtw Level 5 Level 5 (4,580 points)

    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 open thisImageFile

     

                                  tell openedFile

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

                                            rotate to 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 file thisImageFile

              tell application "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.

  • VikingOSX Level 5 Level 5 (4,700 points)

    twtwtw,

     

    Question. I do not see any explicit close of these image files. This must be happening somehow in Applescript or there would be a danger of misadventure once the dynamic file handle allocation maxed out. Is the save command performing an implicit close?

1 2 3 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.