Skip to content

Instantly share code, notes, and snippets.

@mludvig
Created September 28, 2011 05:04
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mludvig/1247034 to your computer and use it in GitHub Desktop.
Save mludvig/1247034 to your computer and use it in GitHub Desktop.
split-image.sh - Split a large image into same-size tiles
#!/bin/bash -e
## split-image.sh - Split a large image into same-size tiles
## Split a large image into same-size tiles
## and optionally convert to a print-ready PDF document
##
## For example split a huge A1-size banner into 8x A4 tiles
## and create a single PDF ready for print.
## Author: Michal Ludvig <mludvig@logix.net.nz>
## http://www.logix.cz/michal/devel
## September 28, 2011
## ====== User configurable variables ======
## A4 @300dpi : 2480x3508
TILE_SIZE="2480x3508"
PDF_PAGE_SIZE="A4"
COLORBITS=8 # Use 8 for 256 color palette, Use 24 for Truecolor
## ====== End of user configurable variables ======
if [ "$1" = "" ]; then
echo "Usage: $0 <input-file-name.img>"
exit 1
fi
INFILE=$1
BNAME=$(basename ${INFILE} | sed 's/\.[^\.]*$//')
echo -en "\e[1;33m==== Processing ${INFILE}, please wait...\e[0m "
convert ${INFILE} -crop ${TILE_SIZE} +repage ${BNAME}-tile-%d.png
echo -e "\e[1;32mDone\e[0m"
ls -l ${BNAME}-tile-*.png
echo -ne '\e[1;34mConvert to PDF? [Y/n]:\e[0m '
read ANS
if [ "${ANS}" == "" -o "${ANS}" == "y" ]; then
BNAME_PDF=${BNAME}-tiled.pdf
echo -en "\e[1;33m==== Converting to ${BNAME_PDF}, please wait...\e[0m "
if [ "${COLORBITS}" == 8 ]; then
PNMFILTER='pnmquant 256'
PNMTOTIFF_OPT=""
else
PNMFILTER='cat'
PNMTOTIFF_OPT="-truecolor"
fi
for i in ${BNAME}-tile-*.png; do
pngtopnm $i | ${PNMFILTER} 2>/dev/null | pnmtotiff ${PNMTOTIFF_OPT} -lzw 2>/dev/null > $(basename $i .png).tiff
done
tiffcp -z lzw ${BNAME}-tile-*.tiff ${BNAME}-tmp.tiff
tiff2pdf -z -p ${PDF_PAGE_SIZE} -c $0 ${BNAME}-tmp.tiff -o ${BNAME_PDF}
rm -f ${BNAME}-*.tiff
echo -e "\e[1;32mDone\e[0m"
ls -l ${BNAME_PDF}
fi
echo -ne '\e[1;34mRemove per-tile PNG files? [Y/n]:\e[0m '
read ANS
if [ "${ANS}" == "" -o "${ANS}" == "y" ]; then
echo -en "\e[1;33m==== Removing ${BNAME}-tile-*.pdf, please wait...\e[0m "
rm -f ${BNAME}-tile-*.png
echo -e "\e[1;32mDone\e[0m"
fi
echo -e '\e[1;32m==== All done ====\e[0m'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment