-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.sh
121 lines (103 loc) · 2.94 KB
/
tile.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/sh
# Author : Ahmed Rafik Radwan
# make sure an image was given
if [ ! $1 ]
then
echo "Must specify The image to tile"
exit 1
fi
# Get image width and height
IMG=$1
IMG_WIDTH=`gm identify -format %w ${IMG}`
IMG_HEIGHT=`gm identify -format %H ${IMG}`
if [ ! $IMG_WIDTH -o ! $IMG_HEIGHT ]
then
echo "File must be an Image"
exit 1
fi
if [ ! $2 ]
then
echo "Must specify at least the tile width"
exit 1
fi
# calculate tiles parameters ( count, width, height)
case "$2" in
-c|--count)
if [ ! $3 ]
then
echo "Must specify at least row tiles count"
exit 1
fi
HORIZONTAL_COUNT=$3
if [ $4 ]
then
VERTICAL_COUNT=$4
else
VERTICAL_COUNT=$3
fi
TILE_WIDTH=`expr $IMG_WIDTH / $HORIZONTAL_COUNT`
if [ `expr $IMG_WIDTH % $HORIZONTAL_COUNT` != 0 ]
then
TILE_WIDTH=`expr $TILE_WIDTH + 1`
fi
TILE_HEIGHT=`expr $IMG_HEIGHT / $VERTICAL_COUNT`
if [ `expr $IMG_HEIGHT % $VERTICAL_COUNT` != 0 ]
then
TILE_HEIGHT=`expr $TILE_HEIGHT + 1`
fi
;;
*)
TILE_WIDTH=$2
if [ $3 ]
then
TILE_HEIGHT=$3
else
TILE_HEIGHT=$2
fi
HORIZONTAL_COUNT=`expr $IMG_WIDTH / $TILE_WIDTH`
if [ `expr $IMG_WIDTH % $TILE_WIDTH` != 0 ]
then
HORIZONTAL_COUNT=`expr $HORIZONTAL_COUNT + 1`
fi
VERTICAL_COUNT=`expr $IMG_HEIGHT / $TILE_HEIGHT`
if [ `expr $IMG_HEIGHT % $TILE_HEIGHT` != 0 ]
then
VERTICAL_COUNT=`expr $VERTICAL_COUNT + 1`
fi
;;
esac
# 1. ProgressBar function
# 1.1 Input is currentState($1) and totalState($2)
function ProgressBar {
# Process data
let _progress=(${1}*100/${2}*100)/100
let _done=(${_progress}*4)/10
let _left=40-$_done
# Build progressbar string lengths
_fill=$(printf "%${_done}s")
_empty=$(printf "%${_left}s")
# Build progressbar strings and print the ProgressBar line
printf "\rProgress : [${_fill// /\#}${_empty// /-}] ${_progress}%% | ${1} of ${2}"
}
TILES_COUNT=`expr $VERTICAL_COUNT \* $HORIZONTAL_COUNT`
TILES_DONE=0
# Create a folder for tiles if not exist
TILES_DIR="$(dirname ${IMG})/$(basename ${IMG})-${TILE_WIDTH}x${TILE_HEIGHT}_tiles"
mkdir -p $TILES_DIR
# Create tiles
INDEX_ROWS=0
while [ $INDEX_ROWS -lt $VERTICAL_COUNT ]
do
INDEX_COLS=0
while [ $INDEX_COLS -lt $HORIZONTAL_COUNT ]
do
TOP_OFFSET=`expr $INDEX_ROWS \* $TILE_HEIGHT`
LEFT_OFFSET=`expr $INDEX_COLS \* $TILE_WIDTH`
`gm convert -crop ${TILE_WIDTH}x${TILE_HEIGHT}+${LEFT_OFFSET}+${TOP_OFFSET} ${IMG} ${TILES_DIR}/tile-${INDEX_ROWS}-${INDEX_COLS}.jpg`
TILES_DONE=`expr $TILES_DONE + 1`
ProgressBar $TILES_DONE $TILES_COUNT
INDEX_COLS=`expr $INDEX_COLS + 1`
done
INDEX_ROWS=`expr $INDEX_ROWS + 1`
done
exit 0