PDA

View Full Version : Photoshop Batch: how to suppress errors and resize based on orientation


Geoff Tani
08 October 2006, 04:09 AM
To clean up a large collection of family photographs, I've been using Photoshop's Automator and Photoshop Elements' batch actions as follows:

1. With Photoshop Automator, I crop and straighten psd images and add a border around them. Then save a new copy as psd.
2. Elements has a predefined batch that converts file formats, so I use it to convert psd files to jpg.

(Versions: Photoshop CS1 (8.0), Elements 2.0)

Both features are handy, but there are few limitations. Would anyone know some workarounds?

1. In Photoshop, I run an Automator action set (crop & straighten, add border, save) on 1,000 images. Every so often Photoshop throws up an error message: "an error occurred." I click OK, and the action continues. IE, there appears to be no real error. Even if there is, in the Run Batch dialog, I have set it to: "log errors to file," which I thought would suppress errors, so that they don't interrupt the action.

The problem for me is that the error messages do displya and interrupt the action. I have to stay near the computer just to dismiss an error message. Is there way to truly suppress these kinds of error messages during the action?

2. I use the Elements format conversion batch, because I cannot find anything like it in Photoshop. In the Elements batch dlg, you select the folder of images, the new format, and go. Also, you can set the dpi of the new file, as well have Elements proportionally resize the new image. For example, you can set the vertical side to be 1500 dpi, and the batch will resize the horizontal side proportionally. Simple and nearly perfect.

The problem is that some images are vertically oriented and some are horizontally oriented. What I want is for Elements to take the longer side -- vertical or horizontal -- and make that 1500 dpi. Since Elements does not do this, I get lots of images that are way too big. Is there any way in Elements to do it?

Or, does Photoshop have a more savvy way of automating format conversion, by which I can get what I want? For example, in Photoshop Automator is it possible for me to make a conditional statement, like: "if vertical side > horzontal side, go to step 2. if not, go to step 3."?

As always, thank you very much for your help!

macorson
12 October 2006, 12:45 AM
For item 1, does the log mentioned in "log errors to file" have a better description of the error?

For item 2, you may want to look into scripting Photoshop:

http://partners.adobe.com/public/developer/photoshop/sdk/index_scripting.html

This allows for more powerful approaches to automating Photoshop tasks. In your post you mention resizing sides to 1500 dpi. Do you mean resize the longest side to 1500 pixels? If so, the methodology for resizing to a certain pixel size via scripting is fairly straightforward: evaluate the width and height to determine which is greater, decide what it would take to scale the greater side to the desired size and use that scale to determine the lesser side. Then resize your image to your new dimensions.

example applescript:

tell application "Adobe Photoshop CS2" --change this to your version of PS

set x to current document
set w to width of x
set h to height of x

if w > h then
set new_w to 1500
set new_h to round (h * (1500 / w))
else
set new_h to 1500
set new_w to round (w * (1500 / h))
end if

resize image x width new_w height new_h

end tell

Geoff Tani
17 October 2006, 07:38 PM
For item 1, does the log mentioned in "log errors to file" have a better description of the error?
For item 2, you may want to look into scripting Photoshop:
Thank you very much for the AppleScript suggestion and sample. I'm starting from almost zero when it comes to AS, but this is a good reason to learn it. (After updating the application reference, I tried your script as is, and nothing happened. I assume I have to fill in the parameters - x, h, w. Anyway, it's a helpful model.)

Regarding item 1, in the error log, there is the following line for each file operated on in the batch:

File: "<file path>: <file name>.psd"
Error: The command "Save" is not currently available. (-25920)

My sense is that this does not apply to the error that interrupts the Automator action. This is because, of the 1000 images in the batch, only 15 or so will cause the interruption error. Also, all the files are actually saved, after being cropped & straightened, despite what the error says.

For my own purposes, I have jotted down the names of the 15 files. I suppose I could just test the automator action on those 15 files.

Again, thanks a lot!

macorson
17 October 2006, 08:10 PM
x, w and h are variables with values that are derived by the script itself so you don't need to (and shouldn't) enter them yourself. The example does, however, assume that there is currently an open document in Photoshop. I could see it doing nothing if you didn't have a photo open at the time. If you're interested, I could expand on the example to do everything I think you want it to do: select a folder of photos (.jpgs?), resize them so that the longest side is 1500 pixels and save a copy in a new format (.psds?), in either the original folder or a new one.

Also, there's a chance that the methods for accessing PS via Applescript in the example may have changed between CS 1 and CS 2. It would probably be more helpful for you to find the CS 1 specific documentation. I think, but am not sure, that the scripting pdfs are installed in the Photoshop directory in the applications folder by default. Look for a directory called "Scripting Guide". If not, they should be on the Adobe site somewhere.

macorson
17 October 2006, 10:47 PM
Just for grins here's a fully automated resize Applecript that works with Photoshop CS 2 and possibly CS 1. If not CS 1 compatible it should be pretty close. The script makes the following assumptions:

1. the images you want to convert are jpegs and are in a folder (not on desktop)
2. you want the new files to be saved as Photoshop files (.psd)
3. you want to resize the image with the longest side being 1500 pixels
4. you want the new files saved in the same folder as the old files


on resize(y)

--get rid of .jpg or .jpeg at the end of file name
if y ends with ".jpg" then
set new_y to (characters 1 thru -5 of y) as string
else if y ends with ".jpeg" then
set new_y to (characters 1 thru -6 of y) as string
else
set new_y to y
end if

--set new file name using .psd extension
set new_y to new_y & ".psd"

tell application "Adobe Photoshop CS2" --change as needed
activate --if you want to watch PS do it's thing
open file y
set x to current document

--get dimensions of current photo
set w to width of x
set h to height of x

--calculate new dimensions based on longest side
if w > h then
set new_w to 1500
set new_h to round (h * (1500 / w))
else
set new_h to 1500
set new_w to round (w * (1500 / h))
end if

--resize current document
resize image x width new_w height new_h

--save it as a Photoshop file (.psd)
save current document in file new_y as Photoshop format appending no extension
close current document

end tell
end resize

tell application "Finder"
activate
set f to (choose folder with prompt "Select folder of photos to convert")

--get every jpeg file in the selected folder
set fls to (every file of folder f whose file type is "JPEG")
repeat with a in fls
--send to resize subroutine
my resize((f & name of a) as string)
end repeat
end tell

Geoff Tani
20 October 2006, 09:23 PM
Just for grins here's a fully automated resize Applecript that works with Photoshop CS 2 and possibly CS 1. If not CS 1 compatible it should be pretty close. The script makes the following assumptions:
Thanks so much for the sample script! I will try it out and let you know what happens.

By the way, when I tried your first script, I did have the image file open while running the script.

macorson
21 October 2006, 12:32 AM
Then it sounds like the scripting divide between CS1 and CS2 is greater than I thought. Going through the CS1 reference you should be able to find equivalent commands to my CS2 script.

Geoff Tani
31 October 2006, 11:16 AM
Thanks again for writing up the AppleScript. It works with Photoshop CS 1. Also, I found the scripting guide for CS 1 that you mentioned.

I have tried to tweak it to do a few things differently, but since I still don't know what I'm doing with AppleScript, could I possibly ask you correct my errors?

There are two problems (#s 3 and 4 below), and in the script itself they are marked with *****text*****

I want the modified script to do the following:
1) convert psd to jpg (I made that change)
2) resize based on the shorter side (I made that change - it's temporarily set to 400)
3) resize based on pixels (your script resizes based on milimeters, and I can't figure out how to change the unit value)
4) resize all files in subfolders, in addition to the selected folder (I added a line about subfolders, but when I run the script, it generates errors)
5) ideally, I would like to save the resized images to a different folder, but is that possible with AppleScript?

I'm so sorry to burden you with this. Any help is greatly appreciated.

Geoff

----------

on resize(y)

--get rid of .jpg or .jpeg at the end of file name
if y ends with ".psd" then
set new_y to (characters 1 thru -5 of y) as string
else if y ends with ".tif" then
set new_y to (characters 1 thru -5 of y) as string
else if y ends with ".tiff" then
set new_y to (characters 1 thru -6 of y) as string
else
set new_y to y
end if

--set new file name using .jpg extension
set new_y to new_y & ".jpg"

tell application "Adobe Photoshop CS" --change as needed
activate --if you want to watch PS do it's thing
open file y
set x to current document

--get dimensions of current photo
set w to width of x
set h to height of x


--calculate new dimensions based on shorter side
if w > h then
set new_h to 400
set new_w to round (w * (400 / h))
else
set new_w to 400
set new_h to round (h * (400 / w))
end if

--resize current document
resize image x width new_w height new_h
*****set unit to pixel*****

--save it as a JPG file (.jpg)
save current document in file new_y as JPEG appending no extension
close current document

end tell
end resize

tell application "Finder"
activate
set f to (choose folder with prompt "Select folder of photos to convert")

--get all files in the selected folder
set fls to (every file of folder f *****and subfolders of folder f*****)
repeat with a in fls
--send to resize subroutine
my resize((f & name of a) as string)
end repeat
end tell

macorson
31 October 2006, 02:42 PM
The resizing should be based on whatever the default units are set as in the preferences. In my case it's pixels. You can change your preferences or see if the CS 1 scripting guide lists a method to chage the units on the fly. I suspect there is.

As far as searching subfolders, you'll need to do a recursive loop through them to find all the files. Try googling for more info on those as I'm sure there are more efficient methods out there than what I do in the rare instances I need to do recursive stuff.

Saving files to other folders is certainly doable. In my example script the line "my resize((f & name of a) as string)" indicates the path where the file is to be saved by the variable f. Change f to whatever you want either by hard coding:

ex: my resize(("MacBookPro:Users:Test:Desktop:" & name of a) as string)

or by choosing a folder:

ex: set dest to (choose folder with prompt "destination folder")
my resize((dest & name of a) as string)

If you choose a destination folder you'll probably want to move the "set dest to..." line outside of the "repeat with a in fls" line or else it will ask you for a destination folder for every single file.

Geoff Tani
09 November 2006, 04:58 AM
Thank you very much for the additional advice. I haven't got the script to work the way I want yet, but I will. I really appreciate the help.