Is there a way to use Image Events to re-size an image to a particular image memory size? In other words, the open image size. I have a script that re-sizes to fit in a box, but it is crude. It is based on a square image size that is small enough to be below my required maximum size when an image is square. This means that when an image is not square, as most images are not, it will be much smaller than necessary in memory size. I want to write one that will make the longest side of an image longer if the short side is shorter, and will maintain proportions. Here’s my old code which is pretty basic Image Events stuff:
on open droppedFiles
tell application “ColorSyncScripting” to set RGBprofile to name of default RGB profile
repeat with this_file in droppedFiles
tell application “Finder”
set filename to name of this_file
set filename to text 1 thru word -2 of filename
set filename to filename & “.jpg”
end tell
set the target_length to 2600 – in pixels. Will make the largest dimension this length.
try
tell application “Image Events”
– start the Image Events application
launch
– open the image file
set this_image to open this_file
– perform action
scale this_image to size target_length
– match color profile
match this_image to destination profile RGBprofile
– save the changes
save this_image as JPEG
– purge the open image data
close this_image
end tell
on error error_message
display dialog error_message
end try
tell application “Finder” to set name of this_file to (“2600” & filename)
end repeat
end open
Here’s the script that resizes images to the same open file memory size. It requires Satimage.osax to get a square root.
-- QuickTime supported image formats
property type_list : {"JPEG", "TIFF", "PNGf", "8BPS", "BMPf", "GIFf", "PDF ", "PICT"}
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png", "psd", "bmp", "gif", "jp2", "pdf", "pict", "pct", "sgi", "tga"}
property typeIDs_list : {"public.jpeg", "public.tiff", "public.png", "com.adobe.photoshop-image", "com.microsoft.bmp", "com.compuserve.gif", "public.jpeg-2000", "com.adobe.pdf", "com.apple.pict", "com.sgi.sgi-image", "com.truevision.tga-image"}
-- This droplet processes files dropped onto the applet
on open these_items
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else
try
set this_extension to the name extension of item_info
on error
set this_extension to ""
end try
try
set this_filetype to the file type of item_info
on error
set this_filetype to ""
end try
try
set this_typeID to the type identifier of item_info
on error
set this_typeID to ""
end try
if (folder of the item_info is false) and (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
process_item(this_item)
end if
end if
end repeat
set front_app to (path to frontmost application as Unicode text)
with timeout of 620 seconds
tell application front_app
display dialog "The files are resized!" giving up after 600
end tell
end timeout
end open
-- this sub-routine processes folders
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else
try
set this_extension to the name extension of item_info
on error
set this_extension to ""
end try
try
set this_filetype to the file type of item_info
on error
set this_filetype to ""
end try
try
set this_typeID to the type identifier of item_info
on error
set this_typeID to ""
end try
if (alias of the item_info is false) and ((this_filetype is in the type_list) or (this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
process_item(this_item)
end if
end if
end repeat
end process_folder
-- this sub-routine processes files
on process_item(this_item)
-- NOTE that the variable this_item is a file reference in alias format
set targetPixels to 6969600
tell application "Image Events"
-- match color profile
tell application "ColorSyncScripting" to set RGBprofile to name of default RGB profile
-- start the Image Events application
launch
-- open the image file
set this_image to open this_item
-- perform action
copy dimensions of this_image to {W, H}
set theDiv to W * H
if targetPixels ≥ theDiv then
else
set xSquared to 6969600 / theDiv
set squareRoot to sqrt xSquared
set newWidth to squareRoot * W
set newWidth to (round newWidth rounding down)
set newHeight to squareRoot * H
set newHeight to (round newHeight rounding down)
set theTarget to newWidth
if newHeight > newWidth then set theTarget to newHeight
-- perform action
scale this_image to size theTarget
end if
match this_image to destination profile RGBprofile
-- save the changes
save this_image as JPEG
-- purge the open image data
close this_image
end tell
end process_item