-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtti.sh
220 lines (210 loc) · 5.25 KB
/
tti.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
: "${WRAP_COLUMN:=80}"
: "${FONT_FILE:=/usr/share/fonts/truetype/unifont/unifont.ttf}"
: "${FONT_SIZE:=25}"
: "${FILL_COLOR:=white}"
: "${BACKGROUND_COLOR:=black}"
: "${OBFUSCATE:=0}"
: "${NOISE_ROUNDS:=2}"
: "${NOISE_TYPE:=Impulse}"
: "${LINES_PER_10000PX:=2}"
: "${LINES_THICNESS:=1}"
: "${LINES_COLOR:=#ff0000}"
: "${SWIRL_DEGREES:=20}"
: "${WRAP_COLUMNS:=32}"
: "${STROKE_COLOR:=random}"
: "${STROKE_WIDTH:=0}"
: "${WAVE_AMPLITUDE:=5x128}"
#
# Generate Image
#
# $1 Text
# $2 Filename
#
function tti_generate_image() {
local output_image imagemagick_options escaped i X_0 X_1 Y_0 Y_1
if [[ $# -lt 1 ]]; then
tti_exit "Error: missing argument(s) for ${FUNCNAME[0]}"
fi
escaped=$(tti_adjust_text "$1")
output_image="$2"
if [[ "$BACKGROUND_COLOR" == "random" ]]; then
BACKGROUND_COLOR=$(tti_get_random_hex_color)
fi
if [[ "$FILL_COLOR" == "random" ]]; then
FILL_COLOR=$(tti_get_random_hex_color)
fi
if [[ "$UNDER_COLOR" == "random" ]]; then
UNDER_COLOR=$(tti_get_random_hex_color)
fi
if [[ "$STROKE_COLOR" == "random" ]]; then
STROKE_COLOR=$(tti_get_random_hex_color)
fi
imagemagick_options=(
-font "$FONT_FILE"
#-size "$IMAGE_SIZE"
-pointsize "$FONT_SIZE"
-fill "$FILL_COLOR"
-background "$BACKGROUND_COLOR"
#-undercolor "$UNDER_COLOR"
#-stroke "$STROKE_COLOR"
#-strokewidth "$STROKE_WIDTH"
#-kerning "$KERNING"
#-interword-spacing "$INTERWORD_SPACING"
#-interline-spacing "$INTERLINE_SPACING"
#-gravity "$GRAVITY"
label:"$escaped"
-flatten
)
if [[ "$OBFUSCATE" != 0 ]]; then
imagemagick_options+=(
-wave "$WAVE_AMPLITUDE"
-rotate -90
-wave "$WAVE_AMPLITUDE"
-rotate +90
-emboss 0x1
)
i=0
while [[ "$i" -lt "$NOISE_ROUNDS" ]]; do
imagemagick_options+=(+noise "$NOISE_TYPE")
((i = i + 1))
done
fi
convert "${imagemagick_options[@]}" "$output_image"
if [[ "$OBFUSCATE" == 0 ]]; then
return;
fi
i=0
tti_get_image_size "$output_image"
lines_number=$((("$LINES_PER_10000PX" * "$WIDTH" * "$HEIGHT") / 10000))
imagemagick_second_round=(
"$output_image"
-background "$BACKGROUND_COLOR"
-flatten
)
while [[ "$i" -lt "$lines_number" ]]; do
if [[ "$LINES_COLOR" == "random" ]]; then
line_color=$(tti_get_random_hex_color)
else
line_color="$LINES_COLOR"
fi
if [[ $(tti_get_random_number "0" $(("$WIDTH" + "$HEIGHT"))) -gt \
"$WIDTH" ]]; then
X_0=0
X_1="$WIDTH"
Y_0=$(tti_get_random_number "0" "$HEIGHT")
Y_1=$(tti_get_random_number "0" "$HEIGHT")
else
X_0=$(tti_get_random_number "0" "$WIDTH")
X_1=$(tti_get_random_number "0" "$WIDTH")
Y_0=0
Y_1="$HEIGHT"
fi
imagemagick_second_round+=(-stroke "$line_color" -strokewidth \
"$LINES_THICNESS")
imagemagick_second_round+=(-draw "line $X_0,$Y_0 $X_1,$Y_1")
((i = i + 1))
done
imagemagick_second_round+=(-swirl "$SWIRL_DEGREES")
convert "${imagemagick_second_round[@]}" "$output_image"
}
#
# Get Random Hex Color
#
function tti_get_random_hex_color() {
local alphabet color char
alphabet="0123456789ABCDEF"
color=""
for i in {0..5}; do
char=${alphabet:$RANDOM % ${#alphabet}:1}
color+=$char
done
printf "#%s" "$color"
}
#
# Get Image Size
#
# $1 Filename
#
function tti_get_image_size() {
if [[ $# -eq 0 ]]; then
tti_exit "Error: missing argument(s) for ${FUNCNAME[0]}"
fi
if [[ ! -f "$1" ]]; then
tti_exit "Error: image not found"
fi
WIDTH=$(identify -format '%w' "$1")
HEIGHT=$(identify -format '%h' "$1")
}
#
# Get Random Number
#
# $1 Min
# $2 Max
#
function tti_get_random_number() {
if [[ $# -le 1 ]]; then
tti_exit "Error: missing argument(s) for ${FUNCNAME[0]}"
fi
shuf -i "$1"-"$2" -n 1
}
#
# Wrap Text
#
# $1 Text
#
function tti_adjust_text() {
if [[ $# -eq 0 ]]; then
tti_exit "Error: missing argument(s) for ${FUNCNAME[0]}"
fi
printf "%s" "$1" | sed -E 's/(\\|@|%)/\\\1/g' | fold -w "$WRAP_COLUMNS"
}
#
# Copy To Clipboard
#
# $1 Filename
#
function tti_copy_to_clipboard() {
if [[ $# -eq 0 ]]; then
tti_exit "Error: missing argument(s) for ${FUNCNAME[0]}"
fi
xclip -selection clipboard -t image/jpeg -i "$1"
}
#
# Exit
#
# $1 Error Message
#
function tti_exit() {
if [[ $# -eq 0 ]]; then
echo "Error: undefined error."
else
echo "$1"
fi
exit 1
}
#
# Text To Image Main
#
# $1 Text
#
function tti_main() {
local buffer output_image
buffer=""
while IFS= read -r line; do
if [[ "$buffer" != "" ]]; then
buffer+=$'\n'
fi
if [[ "$line" == "." ]]; then
break
fi
buffer+="$line"
done
output_image="/tmp/$(date +%s%N | cut -b1-13).jpg"
tti_generate_image "$buffer" "$output_image"
tti_copy_to_clipboard "$output_image"
if [[ -f "$output_image" ]]; then
rm "$output_image"
fi
}
tti_main "$@"