AppleScript: cutting images in half?

Can I split an image in half using AppleScript and save them separately?

+4
source share
2 answers

It would be easier to use ImageMagick . This saves the left half as input-0.png , and the right half as input-1.png :

 convert input.png -crop 50%x100% +repage input.png 

This saves only the right half as right.png :

 convert input.png -gravity east -crop 50%x100% +repage right.png 

+repage removes metadata for the old canvas size. See http://www.imagemagick.org/Usage/crop/ .

You can install ImageMagick with brew install imagemagick or sudo port install imagemagick .

+7
source

You can use Image Events, which comes with Mac OS X to crop the image, if you do not have access to scripting for editing images (as in Photoshop). To get the dimensions of the image, just use something like the following ...

 on GetImageDimensions(TheFile) -- (file path as string) as {width, height} try tell application "Image Events" launch --we have to launch Image Events before we can use it set theImage to open TheFile set theImageDimensions to dimensions of theImage set theImageWidth to item 1 of theImageDimensions set theImageHeight to item 2 of theImageDimensions return {theImageWidth, theImageHeight} end tell on error return {-1, -1} // just in case something goes wrong end try end GetImageDimensions 

... and the command to crop the image is as simple as

 crop pathToFile to dimensions {cropWidth, cropHeight} 

If, by chance, you have Photoshop, then cropping is handled differently:

 crop pathToFile bounds {cropLeft, cropTop, cropRight, cropBottom} 

The command is larger, but these are the required parameters. Other applications are more likely to have a different implementation (probably more like Apple). Simply select your image editing application and re-read the dictionary to find out how it works.

+2
source

All Articles