Use Imagemagick (Homebrew Package) to Automatically Crop a Batch of Images

Howdy folks, I've been trying to learn some how to work with shell scripts lately, so I've been making great use of Homebrew packages to make some basic macros.

My latest one is designed to use Imagemagick (a Homebrew package) to crop multiple images with the same dimensions to a specific area of said images. Here's how it works:

  1. You select the images you wish to crop and trigger the macro.
  2. It then opens one of them to allow you to click and drag the mouse to select the area you want to crop the images to.
  3. Then it temporarily crops that image so it can pull the new image size (in pixels) from the Inspector window and then reverts it back to it's original size.
  4. Next you set click and drag your mouse to select the offset from the top left of the cropped area to the top left of the overall image.
  5. It then crops it again to get the size (again in pixels) to use as the offset coordinates in the shell script, but once again reverts it to it's original size.
  6. It then closes the image and runs the shell script on all of the images originally selected.

Attached is a screenshot showing it in action.

While the macro works quite well for me (and why I posted it here, and not in the Questions and Suggestions category), I'm still interested in everybody's thoughts and ideas if this macro can be improved. This is one that took me a couple of weeks to figure out a reliable way to get the coordinates and offsets, and while I have been able to test it on several different machines, it definitely gave me a lot of issues initially. So I'd be curious to get everybody's feedback on it! Thanks in advance, and if this macro proves useful to anybody else then enjoy!

-Chris

NOTE: This macro has special requirements to run

This macro makes use of the following KM version 10 action(s):
Position next engine window
If you are running a previous version of KM, simply disable or remove said action(s).

This macro requires Homebrew to be installed. Click the link for more info.

This macro requires Imagemagick (a Homebrew package) to be installed. Click the link for more info.

This macro requires the end-user to set a ENV_PATH variable (or specify the path in the shell script). Click the link for more info.

CURRENT VERSION: 1.0 (Monday, January 24, 2022)

v1.0: Initial macro

Screen Recording 2022-01-24 at 20.55.34

Macro screenshot (click to expand/collapse)

01)[AS-F] Images- auto crop batch.kmmacros (51 KB)