-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeld.sh
executable file
·203 lines (176 loc) · 5.66 KB
/
meld.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/sh
# Initialize our own variables:
output="output.jpeg"
verbose=0
top=""
bottom=""
list=""
work_dir=cache/work
oracle_text=
show_help() {
echo "Stuff"
exit
}
title_case() {
echo "$1" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'
}
simplify_text() {
echo "$1" | sed "s/[^[:alnum:]-]//g" | tr '[:upper:]' '[:lower:]'
}
oracle_text_size() {
local text_length=${#1}
local line_count=$(echo $1 | wc -l)
if [ "$line_count" -gt 8 ]
then
echo 16
elif [ "$line_count" -gt 6 ]
then
echo 18
else
if [ "$text_length" -gt 600 ]
then
echo 15
elif [ "$text_length" -gt 500 ]
then
echo 17
elif [ "$text_length" -gt 400 ]
then
echo 19
elif [ "$text_length" -gt 300 ]
then
echo 22
elif [ "$text_length" -gt 200 ]
then
echo 25
else
echo 28
fi
fi
}
card_face_supplier() {
local file="cache/$1.jpeg.txt"
if jq -e ".data[0].$2" "$file" >/dev/null
then
jq ".data[0].$2" "$file" | tr -d '"'
else
local front_face="$(jq ".data[0].card_faces[0].name" "$file")"
if [ "$(simplify_text "$1")" = "$(simplify_text "$front_face")" ]
then
jq ".data[0].card_faces[0].$2" "$file" | tr -d '"'
else
jq ".data[0].card_faces[1].$2" "$file" | tr -d '"'
fi
fi
}
download_card() {
echo "Downloading card details and high res image for $1..."
curl -L -G --header "Accept=*/*" \
--data-urlencode "q=!\"$1\" -set:lea order:release direction:ascending game:paper prefer:oldest" \
https://api.scryfall.com/cards/search \
-o "$2.txt"
if jq -e '.data[0].image_uris.png' "$2.txt" > /dev/null
then
curl -L -G --header "Accept=*/*" \
$(jq '.data[0].image_uris.png' "$2.txt" | tr -d '"') \
-o "$2"
else
local front_face="$(jq '.data[0].card_faces[0].name' "$2.txt")"
if [ "$(simplify_text "$1")" = "$(simplify_text "$front_face")" ]
then
curl -L -G --header "Accept=*/*" \
$(jq '.data[0].card_faces[0].image_uris.png' "$2.txt" | tr -d '"') \
-o "$2"
else
curl -L -G --header "Accept=*/*" \
$(jq '.data[0].card_faces[1].image_uris.png' "$2.txt" | tr -d '"') \
-o "$2"
fi
fi
}
meld_card(){
echo "Generating Proxy for $top $bottom..."
mkdir -p "$work_dir"
top_file="cache/$top.jpeg"
bottom_file="cache/$bottom.jpeg"
if [ ! -f "$top_file" ]
then
download_card "$top" "$top_file"
fi
if [ -z "$bottom" ]
then
convert "$top_file" "$(echo "output/$top.png" | tr " " _)"
else
if [ ! -f "$bottom_file" ]
then
download_card "$bottom" "$bottom_file"
fi
output_file="output/$(echo "$top#$bottom.png" | tr " " _)"
artists="$(card_face_supplier "$top" 'artist') / $(card_face_supplier "$bottom" 'artist')"
convert "$top_file" DividingLine.png -alpha off -compose CopyOpacity -composite $work_dir/tmp.png
convert "$bottom_file" DividingLine.png -alpha off -rotate 180 -compose CopyOpacity -composite $work_dir/tmp2.png
convert $work_dir/tmp.png $work_dir/tmp2.png -alpha set -composite $work_dir/tmp.png
convert $work_dir/tmp.png -pointsize 16 -draw "gravity south fill white text 0,10 '$artists'" "$output_file"
if [ ! -z "$oracle_text" ]
then
echo " - Appending oracle text..."
top_oracle_text="$(card_face_supplier "$top" 'oracle_text')"
bottom_oracle_text="$(card_face_supplier "$bottom" 'oracle_text')"
convert \( -background '#FFF6' -font JaceBeleren-Bold -fill black -pointsize $(oracle_text_size "$top_oracle_text") -size 550x180 \
-bordercolor '#FFFA' -border 10 \
-mattecolor '#FFF9' -frame 5x5+0+2 \
caption:"$top_oracle_text" \) \
"$output_file" +swap -gravity north -geometry +0+130 -composite "$output_file"
convert \( -background '#FFF6' -font JaceBeleren-Bold -fill black -pointsize $(oracle_text_size "$bottom_oracle_text") -size 550x180 \
-bordercolor '#FFFA' -border 10 \
-mattecolor '#FFF9' -frame 5x5+0+2 \
caption:"$bottom_oracle_text" -rotate 180 \) \
"$output_file" +swap -gravity south -geometry +0+130 -composite "$output_file"
fi
#convert "$output_file" CornerMask.png -alpha off -compose CopyOpacity -composite "$output_file"
rm -rf "$work_dir/*"
fi
}
# A POSIX variable
# Reset in case getopts has been used previously in the shell.
OPTIND=1
while getopts "h?f:t:b:o" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
o)
oracle_text="true"
;;
t)
top="$(title_case "$OPTARG")"
;;
b)
bottom="$(title_case "$OPTARG")"
;;
f) list="$OPTARG"
;;
esac
done
shift $((OPTIND-1))
mkdir -p cache
rm -rf output/*
mkdir -p output
if [ -z "$list" ]
then
meld_card
else
while read line
do
top="$(echo $line | cut -f 1 -d =)"
bottom="$(echo $line | cut -f 2 -d =)"
top="$(title_case "$top")"
bottom="$(title_case "$bottom")"
meld_card
done < $list
fi
echo "Generating PDF..."
#https://www.slightlymagic.net/forum/viewtopic.php?f=46&t=1241
#olze » 04 Jun 2009, 16:38
montage -density 297 -tile 3x3 -geometry +2+2 output/*.png output/montage.png
convert output/montag*.png output/montage.pdf