Image cropping is a common task in many workflows, and the Code Runner block in Hunch makes it easy to automate this process for multiple images. This example demonstrates how to use a Bash script with ImageMagick to crop images efficiently within your Hunch workflow.

The script below uses curl to download images from URLs provided by connected input blocks, then utilizes ImageMagick’s convert command to crop each image to specified dimensions. This approach is particularly useful when you need to process multiple images with the same cropping parameters.

Here it is in action in Hunch: Crop an image

#!/bin/bash

# CROP_WIDTH and CROP_HEIGHT define the size of the resulting cropped image
# CROP_X and CROP_Y define the top-left corner of the crop area
# Adjust these variables to change the cropping behavior
# Example: CROP_WIDTH=300 CROP_HEIGHT=200 CROP_X=100 CROP_Y=50
# This would crop a 300x200 area, starting 100 pixels from the left and 50 pixels from the top

CROP_WIDTH=100
CROP_HEIGHT=50
CROP_X=0
CROP_Y=0

# Output format
OUTPUT_FORMAT="png"

i=1
for url in $(hunch input | jq -r '.blocks[] | .files[] | .url'); do
    curl -fsSL "$url" \
        | convert - \
            -crop ${CROP_WIDTH}x${CROP_HEIGHT}+${CROP_X}+${CROP_Y} \
            /output/cropped-$i.$OUTPUT_FORMAT
    ((i++))
done

Detailed Explanation

Let’s break down the script and examine its key components:

Configuration Variables

CROP_WIDTH=100
CROP_HEIGHT=50
CROP_X=0
CROP_Y=0
OUTPUT_FORMAT="png"

These variables define the cropping parameters and output format. You can adjust these to customize the cropping behavior:

  • CROP_WIDTH and CROP_HEIGHT: Define the size of the cropped area
  • CROP_X and CROP_Y: Define the starting point (top-left corner) of the crop
  • OUTPUT_FORMAT: Specifies the format of the output images

Processing Input Blocks

for url in $(hunch input | jq -r '.blocks[] | .files[] | .url'); do

This line is crucial for handling input from Hunch:

  1. hunch input: This command retrieves all input data for the Code Runner block in JSON format.
  2. jq -r '.blocks[] | .files[] | .url': This complex jq command processes the JSON:
    • .blocks[]: Iterates through all input blocks
    • .files[]: For each block, it accesses the ‘files’ array
    • .url: Extracts the URL of each file
    • The -r option outputs raw strings without quotes

This combination allows the script to process multiple input blocks, each potentially containing multiple files.

Downloading and Cropping Images

curl -fsSL "$url" \
    | convert - \
        -crop ${CROP_WIDTH}x${CROP_HEIGHT}+${CROP_X}+${CROP_Y} \
        /output/cropped-$i.$OUTPUT_FORMAT

This section handles the actual image processing:

  1. curl -fsSL "$url": Downloads the image from the URL
    • -f: Fails silently on server errors
    • -s: Silent mode
    • -S: Shows error messages
    • -L: Follows redirects
  2. |: Pipes the downloaded image data directly to ImageMagick
  3. convert -: ImageMagick command to process the image (’-’ reads from stdin)
  4. -crop ${CROP_WIDTH}x${CROP_HEIGHT}+${CROP_X}+${CROP_Y}: Crops the image using the specified dimensions and starting point
  5. /output/cropped-$i.$OUTPUT_FORMAT: Saves the cropped image to the output directory with a numbered filename

Incrementing the Counter

((i++))

This increments the counter i, ensuring each output file has a unique name.

Error Handling

While this script doesn’t include explicit error handling, it uses some best practices:

  • The -f flag in curl will cause the script to fail if an image can’t be downloaded
  • Using set -e at the beginning of the script (not shown here) would cause it to exit on any command failure

Output

The script saves cropped images to the /output directory, which is accessible to downstream blocks in your Hunch workflow. Each image is named cropped-N.png, where N is a number starting from 1.

This script demonstrates the power of combining Hunch’s Code Runner with standard Unix tools and ImageMagick to perform complex image processing tasks. It can be easily modified to perform other image manipulations by changing the ImageMagick commands.