Cutting images in half with ImageMagick

This is not exactly scripting DT, but scripting that is useful for DT.

Here is a BASH script that will cut image files vertically or horizontally in half in the middle. Very useful if you scan in double pages from books and want to cut them in half. It requires ImageMagick, which can be downloaded from ImageMagick, installed easily from DarwinPorts or built yourself. DarwinPorts is easy to use and also means upgrades are a piece of cake. I haven’t used the binary installer from ImageMagick, but imagine it would also be easy.

Save the script as a text file (I call is split.sh) using your favourite text editor (eg TextWrangler, Smultron, SubEthaEdit, Eddie, TextMate, etc.) and make it executable with the following commands from a terminal window open to the same folder:

chmod +x split.sh
rehash

Put the file in the same folder as the images you want to split. Then, from a terminal open to the same folder (I use OpenTerminalHere), and simply type


./split.sh

to execute the file. Change the “split.sh” to whatever you called the file. You need the leading dot-slash when running the script. The script will put your newly split images in a new folder called “output” within the original images folder. It will take some time to execute (about 2 seconds per 1MB original image on my iBook G4 1GHz). The original images are untouched.

Note that I have not tried this in TCSH or other terminals, but I imagine it would work.

Script (vertical cut)

#!/bin/bash
# Script to split images in half using ImageMagick
# By Euan McKay

# You can adjust these settings to suit your needs

FOLDER='output' # set output folder name
mkdir $FOLDER   # create the folder to store split images

INPUT='TIF'     # set extension of image type to split
OUTPUT='tif'    # set extension of image type to save

# You shouldn't need to adjust anything below here

COUNTER=0       # set counter to zero

for ITEM in *.$INPUT        # get items in present folder

do                          # start loop

    data=`identify $ITEM | awk '{print $3}'`                # get image data
    W=`echo $data | sed 's/[^0-9]/ /g' | awk '{print $1}'`  # extract width
    H=`echo $data | sed 's/[^0-9]/ /g' | awk '{print $2}'`  # extract height
    NEWW=$((W/2))                                           # set new width

    LEFT=${FOLDER}'/'${COUNTER}.0.$OUTPUT           # filename of left half
    RIGHT=${FOLDER}'/'${COUNTER}.1.$OUTPUT          # filename of right half

    convert -verbose -crop ${NEWW}x${H} $ITEM $LEFT         # make left half
    convert -verbose -crop ${W}x${H}+${NEWW}-0 $ITEM $RIGHT # make right half

    COUNTER=$((COUNTER+1))  # increment counter

done                        # end loop

You should change the “TIF” in the file to the extension of your images (eg jpg, jpeg, gif, tif, tiff, etc.). This script will probably not work with multipage PDFs, but I haven’t tried. You will probably need to extract pages to individual files first. Remove the “-verbose” tag if you don’t like the terminal telling you what it is doing.

Splitting horizontally
To split images horizontally, change the lines

    NEWW=$((W/2))                                           # set new width

    LEFT=${FOLDER}'/'${COUNTER}.0.$OUTPUT           # filename of left half
    RIGHT=${FOLDER}'/'${COUNTER}.1.$OUTPUT          # filename of right half

    convert -verbose -crop ${NEWW}x${H} $ITEM $LEFT         # make left half
    convert -verbose -crop ${W}x${H}+${NEWW}-0 $ITEM $RIGHT # make right half

to read

    NEWH=$((H/2))                                           # set new height

    TOP=${FOLDER}'/'${COUNTER}.0.$OUTPUT             # filename of top half
    BOTTOM=${FOLDER}'/'${COUNTER}.1.$OUTPUT          # filename of bottom half

    convert -verbose -crop ${W}x${NEWH} $ITEM $TOP           # make top half
    convert -verbose -crop ${W}x${H}+0+${NEWH} $ITEM $BOTTOM # make bottom half

Download
You can download the files here:
Vertical split
Horizontal split

Automator
If anyone can get this working in Automator, that would be great. I can’t seem to get it right.

Warning
Use of this script is at your own risk etc etc.

Acknowledgements
When writing this, I looked at several sources on the internet that may have included this page, but can’t be sure. All kudos where it is due.