Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to resize the longest side of an image Apps
It's easy to put together a batch process in Photoshop to resize all the images in a folder to a certain height or width. However, if some of the photos are portraits and some are landscapes, they end up at different sizes. This script determines which dimension is the longest one and resizes that dimension to the specified number of pixels (set by the user). Note that this hint requires Photoshop, obviously...
set target_size to text returned of (display dialog "Enter target size of longest side in pixels:" default answer 2400)
set raw_folder to choose folder with prompt "Select originals folder:"
set live_folder to choose folder with prompt "Select target folder:"
tell application "Finder"
  set itemList to files in raw_folder
end tell
repeat with an_item in itemList
  tell application "Finder"
    set current to an_item as alias
  end tell
  
  tell application "Adobe Photoshop CS"
    activate
    open current
    set this_image to current document
    set long_side to height of current document
    set long_side_identity to 1
    if width of current document > long_side then set long_side_identity to 2
    if width of current document > long_side then set long_side to width of current document
    if long_side > target_size as integer and long_side_identity = 1 then
      resize image this_image height target_size as integer
    end if
    if long_side > target_size as integer and long_side_identity = 2 then
      resize image this_image width target_size as integer
    end if
    tell current document
      save as JPEG in live_folder
      close
    end tell
  end tell
end repeat
This is set to save the images as JPEGs -- if you want it to save them in some other format, just change the line Save as JPEG in live_folder to Save as TIFF..., Save as Photoshop format..., Save as Photoshop EPS..., etc. (See the AppleScript dictionary for Photoshop for a complete list of formats.)

[robg adds: I haven't tested this one.]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[13,890 views]  

An AppleScript to resize the longest side of an image | 11 comments | Create New Account
Click here to return to the 'An AppleScript to resize the longest side of an image' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to resize the longest side of an image
Authored by: apveenstra on Dec 31, '07 07:46:37AM

Or you could just use the built in Photoshop tool: File -> Automate -> Fit Image...



[ Reply to This | # ]
An AppleScript to resize the longest side of an image
Authored by: patniemeyer on Dec 31, '07 07:58:24AM

Just use 'sips -Z [size]' on the images... it has the same effect.

Be warned though that it replaces the image with the scaled one... so be careful to only work on copies.



[ Reply to This | # ]
ImageMagick
Authored by: fracai on Dec 31, '07 08:13:12AM

Or you can use ImageMagick if you have it installed.

The following will resize an image if it is larger than the given size while retaining the image proportions. If you always want to resize the image (ie. enlarge an image if it is smaller than the given dimensions) omit the ">".

convert -resize "100x100>" image.jpg image-thumb.jpg

---
i am jack's amusing sig file



[ Reply to This | # ]
…or just use sips
Authored by: spicyj on Dec 31, '07 10:44:12AM
This is true, but patniemeyer's suggestion doesn't require installing ImageMagick (sips is an interface to Apple's Image Events processing system).

Just type sips -Z 100 image.jpg --out image-thumb.jpg to achieve the same result as your command.

[ Reply to This | # ]
An AppleScript to resize the longest side of an image
Authored by: sfgecko on Dec 31, '07 10:01:02AM
Just a question...
In Adobe ImageReady CS2, the Image Size setting allows you to constrain by width or height and not enlarge the image if the original size is smaller than the constrain width/height. Is there an equivalent resize tool in Photoshop CS3?

Here's a screenshot from ImageReady (note the Do Not Enlarge checkbox):
http://img106.imageshack.us/img106/6618/imagesizezl3.png

[ Reply to This | # ]
An AppleScript to resize the longest side of an image
Authored by: PeaceFreak on Dec 31, '07 02:36:31PM

Sounds very useful to me but I am a newbie to scripts. Can someone fill me in on how to setup this script? Tried various adjustments but cannot get it to work. Could someone tell me how to modify it for say a longest side of 400 pixels...

I take it the finished script goes in Photoshop/Presets/Scripts...

Thanks in advance!



[ Reply to This | # ]
An AppleScript to resize the longest side of an image
Authored by: microbians on Jan 01, '08 05:03:39AM
I think is better not to use Photoshop to do the resize when you have "Image Events" in the Mac Os X.

Here is a variation on the code found here (http://schinckel.net/2006/03/04/resizing-images-in-applescriptautomator/) but to handdle multiple files:

---------------------------------------------------
global theImage

tell application "Finder"
set sels to selection
end tell

set theResult to display dialog "Select the short side size." default answer "450"
set theAnswer to text returned of theResult
-- Retrieve the resize percentage amount
set theNewWidth to theAnswer as integer

repeat with sel in sels
set theImage to sel as alias

tell application "Finder"
set theImageName to name of theImage as string
set theImageFolder to (folder of theImage) as string
end tell

set theResizedImagePath to theImageFolder & theImageName & "_RESIZED" as string

tell application "Image Events"
launch
set theImage to open theImage

-- This is the tricky part: you need to keep the ratio of the original picture
copy dimensions of theImage to {currentWidth, currentHeight}
set ratio to theNewWidth / currentWidth

tell theImage
scale by factor ratio to size theNewWidth
save in theResizedImagePath
close
end tell
end tell
end repeat
---------------------------------------------------


[ Reply to This | # ]
An AppleScript to resize the longest side of an image
Authored by: Spinnetti on Jan 01, '08 07:45:18AM

Automator has this built into the resize image function, and its really handy. I drag stuff out of iphoto to the desktop, then have the resize image function as a contextual menu item. - should be built into email!



[ Reply to This | # ]
An AppleScript to resize the longest side of an image
Authored by: foilpan on Jan 01, '08 12:50:58PM

i second spicyj's use of sips. it's quicker and easier than these applecsripts, which probably use sips to do the conversions anyway.

for more, check the sips man page



[ Reply to This | # ]
An AppleScript to resize the longest side of an image
Authored by: sparklyballs on Jan 03, '08 06:18:55AM

is it possible to have an automater script that will convert nef files into jpg files but always keep the output file under 1mb ?



[ Reply to This | # ]
An AppleScript to resize the longest side of an image
Authored by: tom stratton on Jan 10, '08 10:23:05AM

An easy way to do this with photoshop batch/actions is to use the

File > Automate > Fit Image... command instead. This allows you to set the maximum size of your images in both directions (to achieve what you are doing set both values to the same number).

Much less trouble!



[ Reply to This | # ]