annotate frontends/src/wix/images/split_card.sh @ 0:8b41eb519075

Files reorganisation
author Goffi <goffi@goffi.org>
date Wed, 29 Dec 2010 01:06:29 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/bin/sh
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #This script split an image with cards, used to split cards from the Tarot deck found on Wikimedia Commons
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
3 #This script work with any resolution on the initial image
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
4 #Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
5
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
6 #This program is free software: you can redistribute it and/or modify
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
7 #it under the terms of the GNU General Public License as published by
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
8 #the Free Software Foundation, either version 3 of the License, or
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
9 #(at your option) any later version.
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
10
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
11 #This program is distributed in the hope that it will be useful,
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
12 #but WITHOUT ANY WARRANTY; without even the implied warranty of
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
13 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
14 #GNU General Public License for more details.
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
15
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
16 #You should have received a copy of the GNU General Public License
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
17 #along with this program. If not, see <http://www.gnu.org/licenses/>.
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
18
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
19 dest_dir=cards
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
20
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
21 get_face_name()
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
22 {
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
23 if [ $1 -le 10 ]
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
24 then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
25 echo $1
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
26 else
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
27 case $1 in
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
28 11) echo valet;;
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
29 12) echo cavalier;;
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
30 13) echo dame;;
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
31 14) echo roi;;
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
32 esac
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
33 fi
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
34 }
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
35
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
36 get_card_name()
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
37 {
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
38 if [ $1 -le 21 ]
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
39 then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
40 echo "atout_$1"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
41 elif [ $1 -eq 22 ]; then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
42 echo "atout_excuse"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
43 elif [ $1 -le 36 ]; then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
44 echo "pique_$(get_face_name $(($1-22)))"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
45 elif [ $1 -le 50 ]; then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
46 echo "coeur_$(get_face_name $(($1-36)))"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
47 elif [ $1 -le 64 ]; then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
48 echo "carreau_$(get_face_name $(($1-50)))"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
49 else
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 echo "trefle_$(get_face_name $(($1-64)))"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
51 fi
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
52 }
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
53
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
54 #We check the version of convert
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
55 CONVERT_VER=`convert --version | grep Version | grep -o "[0-9]\.[0-9]\.[0-9]"`
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
56 CONVERT_MAJOR=`echo $CONVERT_VER | cut -d . -f 1`
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
57 CONVERT_MINOR=`echo $CONVERT_VER | cut -d . -f 2`
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
58 CONVERT_REV=`echo $CONVERT_VER | cut -d . -f 3`
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
59
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
60 if [ $CONVERT_MAJOR -lt 6 -o $CONVERT_MAJOR -eq 6 -a $CONVERT_MINOR -lt 6 ]
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
61 then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
62 echo "ImageMagick convert must be at least version 6.6.0 (current: $CONVERT_VER)"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
63 exit 1
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
64 fi
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
65
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
66 SYNTAXE="Split card image\nsyntaxe: $0 image_to_split.ext"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
67
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 if [ $# -ne 1 ]
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
69 then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
70 echo $SYNTAXE
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
71 exit 1
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
72 fi
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
73
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
74 echo `file -b --mime-type $1` | grep image 2>&1 > /dev/null
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
75
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
76 if [ $? -ne 0 ]
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
78 echo "target file is not an image"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
79 exit 1
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
80 fi
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
81
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
82 current=`pwd`
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
83 #TODO: check directory presence
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
84 #echo "making directory"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if test -e $dest_dir
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
86 then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if test -n "`ls -A $dest_dir`"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
88 then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 echo "$dest_dir directory exists and is not empty !"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
90 exit 1
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
91 fi
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
92 else
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 mkdir $dest_dir
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
94 fi
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
95 echo "splitting cards"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
96 convert Tarotcards.jpg -bordercolor black -crop 14x6@ -fuzz 50% -trim $dest_dir/card_%02d.png 2>/dev/null
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 cd $dest_dir
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
98
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
99 #POST PROCESSING
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
100
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
101 nb_files=`ls -A1 card*png | wc -l`
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
102 num=0
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
103 idx=0
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
104 max_w=0
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
105 max_h=0
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
106 deleted=""
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
107 for file in card*png
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
108 do
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
109 num=$((num+1))
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
110 size=`stat -c%s $file`
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
111 width=`identify -format "%w" $file`
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
112 height=`identify -format "%h" $file`
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
113
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
114 if [ $width -gt $max_w ]
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
115 then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
116 max_w=$width
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
117 fi
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
118
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
119 if [ $height -gt $max_h ]
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
120 then
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
121 max_h=$height
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
122 fi
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
123
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
124 echo -n "post processing file [$file] ($num/$nb_files) | "
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
125 echo -n `echo "scale=2;$num/$nb_files*100" | bc`%
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
126 echo -n "\r"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
127
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
128 if test $size -lt 1000
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
129 then #we delete empty files (areas without card on the initial picture)
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
130 deleted="$deleted$file\n"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
131 rm -f $file
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
132 else
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
133 idx=$((idx+1))
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
134 #We use transparency for the round corners
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
135 mogrify -fuzz 80% -fill none -draw "matte 0,0 floodfill" \
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
136 -draw "matte $((width-1)),0 floodfill"\
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
137 -draw "matte 0,$((height-1)) floodfill"\
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
138 -draw "matte $((width-1)),$((height-1)) floodfill"\
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
139 $file
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
140 #Time to rename the cards
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
141 mv "$file" "$(get_card_name $idx).png"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
142
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
143 fi
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
144 done
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
145 echo "\nEmpty files deleted:\n$deleted"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
146 echo "\nBiggest size: ${max_w}X${max_h}"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
147 cd "$current"
8b41eb519075 Files reorganisation
Goffi <goffi@goffi.org>
parents:
diff changeset
148 echo "DONE :)"