81
|
1 #!/bin/sh |
|
2 #This script split an image with cards, used to split cards from the Tarot deck found on Wikimedia Commons |
|
3 #This script work with any resolution on the initial image |
|
4 |
|
5 dest_dir=cards |
|
6 |
|
7 get_face_name() |
|
8 { |
|
9 if [ $1 -le 10 ] |
|
10 then |
|
11 echo $1 |
|
12 else |
|
13 case $1 in |
|
14 11) echo valet;; |
|
15 12) echo cavalier;; |
|
16 13) echo dame;; |
|
17 14) echo roi;; |
|
18 esac |
|
19 fi |
|
20 } |
|
21 |
|
22 get_card_name() |
|
23 { |
|
24 if [ $1 -le 21 ] |
|
25 then |
|
26 echo "atout_$1" |
|
27 elif [ $1 -eq 22 ]; then |
|
28 echo "atout_excuse" |
|
29 elif [ $1 -le 36 ]; then |
|
30 echo "pique_$(get_face_name $(($1-22)))" |
|
31 elif [ $1 -le 50 ]; then |
|
32 echo "coeur_$(get_face_name $(($1-36)))" |
|
33 elif [ $1 -le 64 ]; then |
|
34 echo "carreau_$(get_face_name $(($1-50)))" |
|
35 else |
|
36 echo "trefle_$(get_face_name $(($1-64)))" |
|
37 fi |
|
38 } |
|
39 |
|
40 current=`pwd` |
|
41 #TODO: check directory presence |
|
42 #echo "making directory" |
|
43 if test -e $dest_dir |
|
44 then |
|
45 if test -n "`ls -A $dest_dir`" |
|
46 then |
|
47 echo "$dest_dir directory exists and is not empty !" |
|
48 exit 1 |
|
49 fi |
|
50 else |
|
51 mkdir $dest_dir |
|
52 fi |
|
53 echo "splitting $dest_dir" |
|
54 convert Tarot$dest_dir.jpg -bordercolor black -crop 14x6@ -fuzz 50% -trim $dest_dir/card_%02d.png 2>/dev/null |
|
55 cd $dest_dir |
|
56 |
|
57 #POST PROCESSING |
|
58 |
|
59 nb_files=`ls -A1 card*png | wc -l` |
|
60 num=0 |
|
61 idx=0 |
|
62 max_w=0 |
|
63 max_h=0 |
|
64 deleted="" |
|
65 for file in card*png |
|
66 do |
|
67 num=$((num+1)) |
|
68 size=`stat -c%s $file` |
|
69 width=`identify -format "%w" $file` |
|
70 height=`identify -format "%h" $file` |
|
71 |
|
72 if [ $width -gt $max_w ] |
|
73 then |
|
74 max_w=$width |
|
75 fi |
|
76 |
|
77 if [ $height -gt $max_h ] |
|
78 then |
|
79 max_h=$height |
|
80 fi |
|
81 |
|
82 echo -n "post processing file [$file] ($num/$nb_files) | " |
|
83 echo -n `echo "scale=2;$num/$nb_files*100" | bc`% |
|
84 echo -n "\r" |
|
85 |
|
86 if test $size -lt 1000 |
|
87 then #we delete empty files (areas without card on the initial picture) |
|
88 deleted="$deleted$file\n" |
|
89 rm -f $file |
|
90 else |
|
91 idx=$((idx+1)) |
|
92 #We use transparency for the round corners |
|
93 mogrify -fuzz 80% -fill none -draw "matte 0,0 floodfill" \ |
|
94 -draw "matte $((width-1)),0 floodfill"\ |
|
95 -draw "matte 0,$((height-1)) floodfill"\ |
|
96 -draw "matte $((width-1)),$((height-1)) floodfill"\ |
|
97 $file |
|
98 #Time to rename the cards |
|
99 mv "$file" "$(get_card_name $idx).png" |
|
100 |
|
101 fi |
|
102 done |
|
103 echo "\nEmpty files deleted:\n$deleted" |
|
104 echo "\nBiggest size: ${max_w}X${max_h}" |
|
105 cd "$current" |
|
106 echo "DONE :)" |