Skip to content

Instantly share code, notes, and snippets.

@rshk
Created November 7, 2012 19:53
Show Gist options
  • Save rshk/4033974 to your computer and use it in GitHub Desktop.
Save rshk/4033974 to your computer and use it in GitHub Desktop.
SVG Tiler
#!/bin/bash
#
# SVG Image Tiler - transform SVG images into PNG tiles
# Copyright (C) 2009 Samuele ~redShadow~ Santi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# Extract given image and convert to png - second version
# Params <file> [ -t|--tile <row> <col> ] [ -d|--dest <dest>] [ -ts|--tile-size <tw> <th> ]
# [ -gs|--grid-size <gw> <gh> ] [ -os|--out-size <ow> <oh> ]
# [ -b|--background <svgcolor> ]
#
FILENAME=
SEL_TILE_C='all'
SEL_TILE_R='all'
TILESIZE_W=1200
TILESIZE_H=600
GRID_W=4
GRID_H=8
OUTPUT_W=
OUTPUT_H=
DESTBASE=
BACKGROUND="ffffff00"
DEBUG_MODE=0
INKSCAPE="inkscape"
function debug(){
if [ "$DEBUG_MODE" == "1" ]; then
echo -e "\033[1mDBG>\033[0m $1" >&2
fi
}
ESC="$( echo -e '\033' )"
print_head(){
cat <<EOF
--------------------------------------------------------------------------------
${ESC}[1;33m __ _ __
______ ______ _/ /_(_) /__ _____
/ ___/ | / / __ \`/ __/ / / _ \/ ___/
(__ )| |/ / /_/ / /_/ / / __/ /
/____/ |___/\__, /\__/_/_/\___/_/
/____/ .SH${ESC}[0m
SVGTiler - Copyright (C) 2009 Samuele ~redShadow~ Santi
This program comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Type \`svgtiler.sh --help' for usage and other info.
--------------------------------------------------------------------------------
EOF
}
show_lic() {
cat <<EOF
SVG Image Tiler - transform SVG images into PNG tiles
Copyright (C) 2009 Samuele ~redShadow~ Santi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
EOF
}
show_help() {
N="$( echo -e '\033[0m' )"
B="$( echo -e '\033[1m' )"
U="$( echo -e '\033[4m' )"
cat <<EOF
SVG Image Tiler - Convert SVG files into PNG tiles"
${B}USAGE${N}"
$0 [-f|--file] <file> [ -t|--tile ${U}row${N} ${U}col${N} ] [ -d|--dest ${U}dest${N}]
[ -ts|--tile-size ${U}tw${N} ${U}th${N} ] [ -gs|--grid-size ${U}gw${N} ${U}gh${N} ]
[ -os|--out-size ${U}ow${N} ${U}oh${N} ] [ -b|--background ${U}svgcolor${N} ]
${B}PARAMS${N}
${U}file.svg${N}
The input SVG filename
-t, --tile ${U}row${N} ${U}col${N}
Specify the selected tile.
You can specify a range with ${U}num${N}-${U}num${N}
or a list of space-separated ${U}num${N}s (remember quotes)
-d, --dest ${U}dest${N}
Specify the output destination. The output path of each tile is created
appending \`${U}row${N}-${U}col${N}.png' to ${U}dest${N}.
Defaults to ${U}file${N} without \`.svg' extension.
-ts, --tile-size ${U}width${N} ${U}height${N}
Specify the tile size. Defaults to: $TILESIZE_W,$TILESIZE_H
-gs, --grid-size ${U}width${N} ${U}height${N}
Specify the grid size, by number of horizontal/vertical tiles.
Defaults to: $GRID_W,$GRID_H
-os, --out-size ${U}width${N} ${U}height${N}
Specify the size of the output PNG image. Defaults to tile size.
You can also use -ow or -oh to set only width/height. The unset
value is auto-determined by keeping aspect ratio.
-b, --background ${U}svgcolor${N}
Specify the background of the image. Defaults to: $BACKGROUND.
-h, --help
Show this help and exit.
--license
Show license and exit.
--debug
Enable debugging mode.
${B}AUTHOR${N}
Samuele ~redShadow~ Santi
[email protected]
http://www.hackzine.org
${B}LICENSE${N}
EOF
show_lic
exit
}
if [ "$1" == "" ] || [ "$1" == "--help" ]; then
show_help
exit
fi
if [ "$1" == '--license' ] || [ "$1" == '--licence' ]; then
show_lic
exit
fi
print_head
debug "*** Debug mode is ON ***"
## Load parameters -------------------------------------------------------------
while [ "$1" != "" ]; do
case "$1" in
"-t"|"--tile")
SEL_TILE_R="$2"
SEL_TILE_C="$3"
shift 2
;;
"-d"|"--dest")
DESTBASE="$2"
shift
;;
"-ts"|"--tile-size")
TILESIZE_W="$2"
TILESIZE_H="$3"
shift 2
;;
"-gs"|"--grid-size")
GRID_W="$2"
GRID_H="$3"
shift 2
;;
"-os"|"--out-size")
OUTPUT_W="$2"
OUTPUT_H="$3"
shift 2
;;
"-ow"|"--out-width")
OUTPUT_W="$2"
shift
;;
"-oh"|"--out-height")
OUTPUT_H="$2"
shift
;;
"-b"|"--background")
BACKGROUND="$2"
shift
;;
"--debug")
DEBUG_MODE=1
;;
"-h"|"--help")
show_help
exit
;;
"--license"|"--licence")
show_lic
exit
;;
"--inkscape")
INKSCAPE="$2"
shift
;;
"-f"|"--file")
FILENAME="$2"
shift
;;
"-"*)
echo "Unknown argument: $1"
;;
*)
FILENAME="$1"
;;
esac
shift
done
## Process parameters ----------------------------------------------------------
if [ "$DESTBASE" == "" ]; then
DESTBASE="`echo "$FILENAME" | sed "s/\.[^\.]*\$//"`_"
fi
debug "Input filename: $FILENAME"
debug "Selected tile: R=$SEL_TILE_R C=$SEL_TILE_C"
debug "Destination base: $DESTBASE"
debug "Using inkscape at: $INKSCAPE (`which "$INKSCAPE"`)"
## Check if destination directory exists or not
DESTDIR="$( dirname -- "$DESTBASE" )"
if [ ! -e "$DESTDIR" ]; then
debug " Creating destination directory: $DESTDIR"
mkdir -p -- "$DESTDIR"
fi
debug "Tile size: $TILESIZE_W,$TILESIZE_H"
debug "Grid size: $GRID_W,$GRID_H"
if [ "$OUTPUT_W" != "" ]; then
debug "Output width: $OUTPUT_W"
else
debug "Output width: auto"
fi
if [ "$OUTPUT_H" != "" ]; then
debug "Output height: $OUTPUT_H"
else
debug "Output height: auto"
fi
debug "Background color: $BACKGROUND"
case "$SEL_TILE_R" in
'all')
debug "Selected all rows"
ROWS="` seq -s " " 0 $[ $GRID_H -1 ]`"
;;
*"-"*)
debug "Selected range of rows: $SEL_TILE_R"
ROWS="`echo "$SEL_TILE_R" | sed "s/-/ /" | xargs seq -s " " `"
;;
*" "*)
debug "Selected list of rows: $SEL_TILE_R"
ROWS="$SEL_TILE_R"
;;
*)
debug "Selected single row: $SEL_TILE_R"
ROWS="$SEL_TILE_R"
;;
esac
debug " Selected Rows: $ROWS"
case "$SEL_TILE_C" in
'all')
debug "Selected all columns"
COLS="` seq -s " " 0 $[ $GRID_W -1 ]`"
;;
*"-"*)
debug "Selected range of columns: $SEL_TILE_C"
COLS="`echo "$SEL_TILE_C" | sed "s/-/ /" | xargs seq -s " "`"
;;
*" "*)
debug "Selected list of columns: $SEL_TILE_C"
COLS="$SEL_TILE_C"
;;
*)
debug "Selected single column: $SEL_TILE_C"
COLS="$SEL_TILE_C"
;;
esac
debug " Selected Columns: $COLS"
for ROW in $ROWS; do
for COL in $COLS; do
DESTFILE="${DESTBASE}${ROW}-${COL}.png"
if [ "$DEBUG_MODE" == "1" ]; then
echo "Extracting Tile r=$ROW c=$COL ..."
else
echo -n "Extracting Tile r=$ROW c=$COL to $DESTFILE ... "
fi
# Create area
X0=$[ $COL * $TILESIZE_W ]
Y0=$[ ( $GRID_H - ( $ROW + 1 ) ) * $TILESIZE_H ]
X1=$[ $X0 + $TILESIZE_W ]
Y1=$[ $Y0 + $TILESIZE_H ]
debug "Tile geometry: $X0:$Y0:$X1:$Y1"
debug "Destination file: $DESTFILE"
COMMAND="$INKSCAPE --without-gui"
COMMAND="$COMMAND --file=$FILENAME"
COMMAND="$COMMAND --export-png=$DESTFILE"
COMMAND="$COMMAND --export-area=$X0:$Y0:$X1:$Y1"
if [ "$BACKGROUND" != "" ]; then
COMMAND="$COMMAND --export-background=$BACKGROUND"
fi
if [ "$OUTPUT_W" != "" ]; then
COMMAND="$COMMAND --export-width=$OUTPUT_W"
fi
if [ "$OUTPUT_H" != "" ]; then
COMMAND="$COMMAND --export-height=$OUTPUT_H"
fi
debug "Command: $COMMAND"
if [ "$DEBUG_MODE" == "1" ]; then
$COMMAND && echo "OK" || echo "FAILED: $?"
else
$COMMAND &>/dev/null && echo "OK" || echo "FAILED"
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment