Manipulating Images
Image Events is a scriptable background app in OS X that can be used to automate the manipulation of images without the need for a fully-featured image editor. You can use Image Events to:
Read image properties
Flip and rotate images
Crop and add padding to images
Resize images
Convert images from one type to another
The Image Events app is located in /System/Library/CoreServices/. You can access its dictionary from the Library palette in Script Editor. See Opening a Scripting Dictionary.
The Image Events Workflow
To manipulate an image with Image Events, a script typically performs the following sequential steps:
Open the Image Events app.
Open an image file.
Access image properties or manipulate the image.
Save the modified image as a new image file or overwriting the original image file.
Close the image.
Opening an Image
An image must be opened before Image Events can interact with it. To open an image, use the open command and provide the image’s path, as shown in Listing 38-1.
APPLESCRIPT
-- Prompt for an imageset theImageFile to choose file of type "public.image" with prompt ""-- Launch Image Events and open the imagetell application "Image Events"launchopen theImageFileend tell--> Result: image "My Image.png" of application "Image Events"
The result of the open command is an image object, the newly opened image. Since Image Events is a background app, opening an image produces no visible changes onscreen—you won’t actually see the opened image.
Reading Image Properties
Like all scriptable objects, images have attributes that define them, such as dimensions, color space, and resolution. The image class in the Image Events scripting dictionary contains a variety of properties for key attributes. Listing 38-2 shows how to access some of these properties. First, it retrieves a record of available properties for a selected image. Next, it retrieves some individual properties.
APPLESCRIPT
-- Prompt for an imageset theImageFile to choose file of type "public.image" with prompt ""-- Launch Image Eventstell application "Image Events"launch-- Open the imageset theImage to open theImageFile-- Read the image's propertiestell theImageproperties--> {color space:RGB, image file:file "Macintosh HD:Users:YourUserName:Desktop:My Image.png" of application "Image Events", bit depth:millions of colors, dimensions:{293, 252}, location:folder "Macintosh HD:Users:YourUserName:Desktop:" of application "Image Events", embedded profile:profile "Thunderbolt Display" of image "My Image.png" of application "Image Events", file type:PNG, class:image, name:"My Image.png", resolution:{72.0, 72.0}}-- Read the image's resolutionresolution--> {72.0, 72.0}-- Read the image's typefile type--> PNG-- Read the name of the image's embedded profilename of embedded profile--> "Thunderbolt Display"end tellend tell
Flipping an Image
The flip command reverses the axis of an image. It has two options for the required parameter: horizontal for changing the axis of the image on a horizontal plane, and vertical for changing the axis of the image on a vertical plane. Listing 38-3 flips an image both horizontally and vertically.
APPLESCRIPT
-- Prompt for an imageset theImageFile to choose file of type "public.image" with prompt ""-- Locate an output folderset theOutputFolder to (path to desktop folder as string)-- Launch Image Eventstell application "Image Events"launch-- Open the imageset theImage to open theImageFiletell theImage-- Determine a save name for the imageset theName to nameset theSaveName to "temp-" & theName-- Flip the image horizontallyflip with horizontal-- Flip the image verticallyflip with vertical-- Save the image to the output folder, using the save namesave as file type in (theOutputFolder & theSaveName)-- Close the imagecloseend tellend tell
Rotating an Image
The rotate command rotates an image around its center point. To rotate an image clockwise, provide the command’s to angle parameter with an integer value between 1 to 359 (see Listing 38-4). To rotate an image counter-clockwise, provide a negative value, such as -90.
APPLESCRIPT
-- Prompt for an imageset theImageFile to choose file of type "public.image" with prompt ""-- Locate an output folderset theOutputFolder to (path to desktop folder as string)-- Launch Image Eventstell application "Image Events"launch-- Open the imageset theImage to open theImageFiletell theImage-- Determine a save name for the imageset theName to nameset theSaveName to "temp-" & theName-- Rotate an image 45 degreesrotate to angle 45-- Save the image to the output folder, using the save namesave as file type in (theOutputFolder & theSaveName)-- Close the imagecloseend tellend tell
Scaling an Image
Scaling an image proportionally increases or decreases its dimensions. The scale command can resize images in one of two ways:
To scale an image by percentage, provide a decimal value for the
by factorparameter. The value1is equivalent to 100%. The value.5is 50%. The value1.5is 150% and so on.Use the following formula to determine the scaling factor:
«percentage» * .01To scale an image to a specific size, provide an integer value for the
to sizeparameter. This value indicates the maximum number of pixels for the resized image on its longest side.
Scaling doesn’t change the resolution of an image. For example, a 72 dpi image that has been scaled to 50% of its original dimensions still has a resolution of 72 dpi.
Listing 38-5 demonstrates how to resize an image. It can scale by percentage or pixels, depending on the value of a Boolean variable.
APPLESCRIPT
-- Prompt for an imageset theImageFile to choose file of type "public.image" with prompt ""-- Locate an output folderset theOutputFolder to (path to desktop folder as string)-- To scale by percentage, set this value to true. To scale to a specific size, set it to false.set scaleByPercentage to true-- Launch Image Eventstell application "Image Events"launch-- Open the imageset theImage to open theImageFiletell theImage-- Determine a save name for the imageset theName to nameset theSaveName to "temp-" & theName-- Scale the image by 50%if scaleByPercentage = true thenscale by factor 0.5-- Scale the image to 100px on its longese sideelsescale to size 100end if-- Save the image to the output folder, using the save namesave as file type in (theOutputFolder & theSaveName)-- Close the imagecloseend tellend tell
Cropping an Image
Cropping an image removes pixels around all of its sides, centering the remaining area. The to dimensions required parameter takes a list of two integers: the new width and height, in pixels. In Listing 38-6, an image is cropped to 100 by 100 pixels.
APPLESCRIPT
-- Prompt for an imageset theImageFile to choose file of type "public.image" with prompt ""-- Locate an output folderset theOutputFolder to (path to desktop folder as string)-- Launch Image Eventstell application "Image Events"launch-- Open the imageset theImage to open theImageFiletell theImage-- Determine a save name for the imageset theName to nameset theSaveName to "temp-" & theName-- Crop the image to 100px by 100pxcrop to dimensions {100, 100}-- Save the image to the output folder, using the save namesave as file type in (theOutputFolder & theSaveName)-- Close the imagecloseend tellend tell
Padding an Image
Padding an image adds space around its sides. It’s essentially the reverse of cropping an image, although negative padding an image produces cropping. The to dimensions required parameter takes a list of two integers: the new width and height, in pixels. The optional with pad color parameter can be used to specify the color of the padding. In Listing 38-7, 20 pixels of padding is added around an image.
APPLESCRIPT
-- Prompt for an imageset theImageFile to choose file of type "public.image" with prompt ""-- Prompt for a colorset theColor to choose color-- Locate an output folderset theOutputFolder to (path to desktop folder as string)-- Launch Image Eventstell application "Image Events"launch-- Open the imageset theImage to open theImageFiletell theImage-- Determine a save name for the imageset theName to nameset theSaveName to "temp-" & theName-- Get the current dimensions of the imageset {theWidth, theHeight} to dimensions-- Pad the image by 20 pixels on all sidespad to dimensions {theWidth + 20, theHeight + 20} with pad color theColor-- Save the image to the output folder, using the save namesave as file type in (theOutputFolder & theSaveName)-- Close the imagecloseend tellend tell
Converting an Image from One Type to Another
To convert an image from one type to another, open it and save it in another format. Listing 38-8 saves a chosen image in .jpg, .psd, and .tif format.
APPLESCRIPT
-- Prompt for an imageset theImageFile to choose file of type "public.image" with prompt ""-- Locate an output folderset theOutputFolder to (path to desktop folder as string)-- Launch Image Eventstell application "Image Events"launch-- Open the imageset theImage to open theImageFiletell theImage-- Save the image as a .jpgsave as JPEG in (theOutputFolder & "temp-conversion-output.jpg")-- Save the image as a .psdsave as PSD in (theOutputFolder & "temp-conversion-output.psd")-- Save the image as a .tifsave as TIFF in (theOutputFolder & "temp-conversion-output.tif")-- Close the imagecloseend tellend tell
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-06-13
