Skip to content

Instantly share code, notes, and snippets.

@metalrufflez
Last active January 20, 2018 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metalrufflez/5f8e682cdf472bac8ff947936df24399 to your computer and use it in GitHub Desktop.
Save metalrufflez/5f8e682cdf472bac8ff947936df24399 to your computer and use it in GitHub Desktop.
Slice Print and Play pages into individual images.
#!/bin/bash
### Using ImageMagick, slices an image in equal parts according to coordinates and column/row numbers
### Useful when slicing images from Print and Play games to later feed to Online Print services
# This is super badly written. Will revise later
function help() {
echo
echo "$(basename $0) -h INT -w INT -H INT -V INT -c INT -r INT -i FILE/DIR -o DIR [-f FILE]"
echo "$(basename $0) -C INTxINT+INT+INT -c INT -r INT -i FILE/DIR -o DIR [-f FILE]"
echo
echo "Mandatory Options"
echo -e "-h\t--height\t\t\tSliced image height"
echo -e "-w\t--width\t\t\t\tSliced image width"
echo -e "-V\t--vertical_offset\t\tInitial vertical offset from the border"
echo -e "-H\t--horizontal_offset\t\tInitial horizontal offset from the border"
echo -e "-c\t--columns\t\t\tNumber of columns of images"
echo -e "-r\t--rows\t\t\t\tNumber of rows of images"
echo -e "-i\t--input\t\t\t\tInput file or directory"
echo -e "\t\t\t\t\t(will run for each image in the directory if directory)"
echo -e "-o\t--output_dir\t\t\tOutput directory."
echo
echo "Optional Options"
echo -e "-C\t--coordinates\t\t\tPass a string in 'HEIGHTxWIDTH+HORIZONTAL_OFFSET+VERTICAL_OFFSET' format"
echo -e "-o\t--output_filename\t\tOutput filename. Will assume 'sliced' if ommited."
echo -e "\t--verbose\t\t\tEnable imagemagick verbosity"
echo -e "\t--debug\t\t\t\tDebug mode, print variables and commands and not execute them"
echo -e "\t--help\t\t\t\tDisplays this help"
}
# Control Variables
ren="^[0-9]+$"
re_coord="^[0-9]+x[0-9]+\+[0-9]+\+[0-9]+$"
counter=1
if [[ $# -eq 0 ]]; then
help
exit 1
fi
# Read Parameters
while [[ $# -gt 0 ]]; do
key=$1
case $key in
--help) help
exit 0
;;
--debug) dry_run="echo"
shift
;;
--verbose) verbose="-verbose"
shift
;;
-i|--input) input="$2"
shift
shift
;;
-d|--output_dir) output_dir="${2%%/*}"
shift
shift
;;
-f|--output_filename) output_filename="$2"
shift
shift
;;
-w|--width) width="$2"
shift
shift
;;
-h|--height) height="$2"
shift
shift
;;
-r|--rows) row_count="$2"
shift
shift
;;
-c|--columns) column_count="$2"
shift
shift
;;
-H|--horizontal_offset) initial_horizontal_offset="$2"
shift
shift
;;
-V|--vertical_offset) initial_vertical_offset="$2"
shift
shift
;;
-C|--coordinates) coordinates="$2"
shift
shift
;;
*) echo "$key - Invalid Option"
help
exit 1
;;
esac
done
# Safety Checks
## Checking either if coordinates or its separate counterparts exist
if ! [[ $coordinates =~ $re_coord ]]; then
if ! [[ "$width" =~ $ren ]] || ! [[ "$height" =~ $ren ]] || ! [[ "$initial_horizontal_offset" =~ $ren ]] || ! [[ "$initial_vertical_offset" =~ $ren ]]; then
echo "Please define coordinates in the 'HEIGHTxWIDTH+HORIZONTAL_OFFSET+VERTICAL_OFFSET' format"
echo "Or individual width, height, and its initial offsets"
exit 1
fi
else
width=$(echo $coordinates | cut -d'x' -f1)
height=$(echo $coordinates | cut -d'x' -f2 | cut -d'+' -f1)
initial_horizontal_offset=$(echo $coordinates | cut -d'+' -f2)
initial_vertical_offset=$(echo $coordinates | cut -d'+' -f3)
fi
## Check if row and column number as correctly declared
if ! [[ "$row_count" =~ $ren ]] || ! [[ "$column_count" =~ $ren ]]; then
echo "Please define valid integers for Row/Column count"
exit 1
fi
## Check if output dir is valid
if ! [[ -d "$output_dir" ]]; then
echo "Please provide a valid output directory"
exit 1
fi
## Assume default filename
if [[ -z "$output_filename" ]]; then
output_filename="sliced"
fi
## Input file/directory 'intelligence'
## If a valid directory, recurse through each file in the dir
## If a valid file, run only for that file
if [[ -z "$input" ]]; then
echo "Please provide a valid input file or directory"
exit 1
elif [[ -d "$input" ]]; then
while_input=$(ls $input/*)
elif [[ -f "$input" ]]; then
while_input=$input
else
echo "$input is not a valid file or directory"
exit 1
fi
# Debug info
if ! [[ -z $dry_run ]]; then
echo
echo "coordinates = $coordinates"
echo "width = $width"
echo "height = $height"
echo "horizontal_offset = $initial_horizontal_offset"
echo "vertical_offset = $initial_vertical_offset"
echo "row count = $row_count"
echo "column count = $column_count"
echo "input = $input"
echo "output_dir = $output_dir"
echo "output_filename = $output_filename"
echo
fi
# Program start
while read file; do
if ! file "$file" | egrep -q 'image|bitmap'; then
echo "$file is not a valid image. Aborting"
exit 1
fi
file_ext=${file##*.}
for column in $(seq 1 $column_count); do
for row in $(seq 1 $row_count); do
let "horizontal_offset=$initial_horizontal_offset+$width*($row-1)"
let "vertical_offset=$initial_vertical_offset+$height*($column-1)"
output="$output_dir/$output_filename-$(printf %02d $counter).$file_ext"
echo "Slicing $file into $output"
$dry_run convert $verbose "$file" -crop "${width}x${height}+${horizontal_offset}+${vertical_offset}" $output
let "counter=$counter+1"
done
done
done < <(echo "$while_input")
@metalrufflez
Copy link
Author

metalrufflez commented Jan 20, 2018

Usage examples:

Using coordinates, single input file output filename (verbosity enabled)
./pnp_slicer.sh --verbose -C "750x1050+150+75" -i pages/abilities-0.png -d sliced_images -f abilities -c 3 -r 3

Using separate coordinates, directory input and omitting output filename (will assume 'sliced')
./pnp_slicer.sh -w 750 -h 1050 -H 150 -V 75 -i pages -d sliced_images -c 3 -r 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment