Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Trim images with too much whitespace and then add a border of a set value
#!/bin/bash
#based on code found here:
#http://jcornuz.wordpress.com/2007/10/26/imagemagick-and-bash-batch-power/
#This script takes an image with a lot of extra border, trims it down
#and then re-adds a 20 pixel border evenly around the edge.
#REQUIRES IMAGEMAGICK AND GHOSTSCRIPT
#the tiles will be put in a directory called...
MYDIR="PROCESSED"
#If the destination folder doesn't exist, make it.
if [ ! -d ./${MYDIR} ]; then mkdir ./${MYDIR}; fi
# processes all pngs
#if there are png files found, then for each of them
if ls | grep png; then for p in *.png; do
echo "Processing $p"
#Trim it, then add a border,
#then save it to a new directory with an
#appended file name.
convert \( $p -fuzz 1% -trim \) \
-bordercolor white -border 20x20 +repage \
./PROCESSED/${p/.png}_clean.png
done
fi
#process all the PDFs
#if there are pdf files found, then for each of them
if ls | grep pdf; then for d in *.pdf; do
echo "Processing $d"
#Open it at an image density of 250px/in,
#trim it, then add a border.
#Save it to a new directory with an
#appended file name.
convert \( \( -density 250 $d \) -fuzz 1% -trim \) \
-bordercolor white -border 20x20 +repage \
./PROCESSED/${d/.pdf}_clean.png
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment