bzr branch
http://gegoxaren.bato24.eu/bzr/useful/trunk-1
22
by Gustav Hartvigsson
* useful.inc.sh |
1 |
#!/usr/bin/env bash
|
2 |
####
|
|
3 |
# FILE NAME process_text_to_image.sh
|
|
4 |
#
|
|
27
by Gustav Hartvigsson
Added copyright information to all files. |
5 |
# Authors:
|
6 |
# Gustav Hartvigsson 2024
|
|
7 |
# Distributed under the Cool Licence 1.1
|
|
8 |
#
|
|
22
by Gustav Hartvigsson
* useful.inc.sh |
9 |
# Changes
|
10 |
#
|
|
11 |
# 2018-09-03:
|
|
12 |
# * added --no-header for when you want to use your own pandoc header
|
|
13 |
#
|
|
14 |
# 2018-09-22
|
|
15 |
# * Fixed --no-header...
|
|
16 |
# Seemed to have forgotten the "$" infront of the variable.
|
|
17 |
#
|
|
18 |
# 2021-01-13
|
|
19 |
# * fixed up the if statments.
|
|
20 |
#
|
|
21 |
# 2024-07-10
|
|
22 |
# * Added sanity check
|
|
23 |
# * General logic fixes.
|
|
24 |
# * Added --author argument
|
|
25 |
# * Fixed help message formating
|
|
26 |
####
|
|
27 |
||
28 |
__DPI=300 |
|
29 |
||
30 |
__IN_FILE= |
|
31 |
__OUT_FILE=big_image |
|
32 |
__PERSERVE_TMP=false |
|
33 |
__INVERT_COLOURS=false |
|
34 |
__NO_PANDOC_HEADER=false |
|
35 |
||
36 |
__CWD=$PWD |
|
37 |
||
38 |
__PANDOC_HEADER=" |
|
39 |
geometry: margin=0.5cm
|
|
40 |
papersize: a5
|
|
41 |
mainfont: DejaVu Serif
|
|
42 |
fontsize: 12pt
|
|
43 |
"
|
|
44 |
||
45 |
__TITLE="" |
|
46 |
__AUTHOR="" |
|
47 |
||
48 |
__SANITY=true |
|
49 |
||
50 |
||
51 |
__SCRIPT_ROOT=$(dirname $(readlink -f $0)) |
|
52 |
source $__SCRIPT_ROOT/useful.inc.sh |
|
53 |
||
54 |
function __usage () { |
|
55 |
|
|
56 |
echo "process_text_to_image.sh - Takes one text file and convernts it to a single" |
|
57 |
echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop" |
|
58 |
echo "" |
|
59 |
echo "!IMPORTANT! The folder \"./tmp/\" in the current working directory will be" |
|
60 |
echo " used as a temporary storage, and may be deleted, along with it's" |
|
61 |
echo " contents!" |
|
62 |
echo "" |
|
63 |
echo "---------------------" |
|
64 |
echo "" |
|
65 |
echo "-h --help" |
|
66 |
echo " Print this help message" |
|
67 |
echo "" |
|
68 |
echo "-i <file> --input <file>" |
|
69 |
echo " The file to use to convert to an image. " |
|
70 |
echo "" |
|
71 |
echo "-o <file> --output <file>" |
|
72 |
echo " The image to output to. (Default=big_image.png)" |
|
73 |
echo "" |
|
74 |
echo "-d <integer> --dpi <integer>" |
|
75 |
echo " Set the dpi of the intermediate image relative to an a5 paper." |
|
76 |
echo " (Default=300)" |
|
77 |
echo "" |
|
78 |
echo "-p --perserve" |
|
79 |
echo " Do not delete the TMP folder." |
|
80 |
echo "" |
|
81 |
echo "--invert" |
|
82 |
echo " Invert the colours of the final image." |
|
83 |
echo "" |
|
84 |
echo "-t \"name\" --title \"name\"" |
|
85 |
echo " Set the title on the the title page." |
|
86 |
echo "" |
|
87 |
echo "-a \"name\" --author \"name\"" |
|
88 |
echo " Set an author to the title page." |
|
89 |
echo "" |
|
90 |
echo "--no-header" |
|
91 |
echo " Do not insert the pandoc header. (Default:" |
|
92 |
echo "$__PANDOC_HEADER" |
|
93 |
echo ")" |
|
94 |
echo "" |
|
95 |
echo "---------------------" |
|
96 |
echo "" |
|
97 |
echo "If you are getting an error from convert that the height or width exeeds" |
|
98 |
echo "some value, you may want to check the ImageMagick policy.xml file." |
|
99 |
echo "" |
|
100 |
echo "The path to ImageMagick policy file is:" |
|
101 |
convert -list policy | grep .xml |
|
102 |
echo "" |
|
103 |
echo "---------------------" |
|
104 |
}
|
|
105 |
||
106 |
||
107 |
function __sanity_check () { |
|
108 |
# Check that we have the tools needed. |
|
109 |
__find_tool pandoc |
|
110 |
__find_tool xelatex |
|
111 |
__find_tool convert |
|
112 |
__find_tool pdftoppm |
|
113 |
__find_tool pdfcrop |
|
114 |
||
115 |
if [[ $__SANITY == false ]]; then |
|
116 |
echo "" |
|
117 |
echo "Please install the missing tools." |
|
118 |
echo "" |
|
119 |
exit 1 |
|
120 |
fi |
|
121 |
}
|
|
122 |
||
123 |
function __main () { |
|
124 |
# FIXME: Split the functionality out of the main function. |
|
125 |
# FIXME: Use mkdtemp instead of the folder we are in. |
|
126 |
__parse_args "${@}" |
|
127 |
__sanity_check
|
|
128 |
|
|
129 |
echo "__IN_FILE\: $__IN_FILE" |
|
130 |
echo "__OUT_FILE\: $__OUT_FILE" |
|
131 |
echo "CWD\: $__CWD" |
|
132 |
echo "__DPI: $__DPI" |
|
133 |
|
|
134 |
if [[ ! -e "$__CWD/$__IN_FILE" ]] || [[ -z $__IN_FILE ]] |
|
135 |
then |
|
136 |
echo "The provided <infile> does not exit." |
|
137 |
echo "" |
|
138 |
exit 1 |
|
139 |
fi |
|
140 |
|
|
141 |
# first we create a temp folder. |
|
142 |
mkdir -p "$__CWD/tmp" |
|
143 |
|
|
144 |
#next we want to copy our file into it. |
|
145 |
cp "$__CWD/$__IN_FILE" "$__CWD/tmp/text.txt" |
|
146 |
cd "$__CWD/tmp" |
|
147 |
|
|
148 |
# Now we can start the work for this. |
|
149 |
if [[ $__NO_PANDOC_HEADER == false ]] |
|
150 |
then |
|
151 |
# FIXME: This is cursed. |
|
152 |
# We add a special header to the file to make it pandoc know what to do. |
|
153 |
# |
|
154 |
# The header is built from the bottom up. The input text at the bottom, and |
|
155 |
# the rest of the "elements" added above. |
|
156 |
|
|
157 |
printf '%s\n' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt" |
|
158 |
if [[ ! -z $__TITLE ]]; then |
|
159 |
printf '%s\n' "title: ${__TITLE}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt" |
|
160 |
fi |
|
161 |
||
162 |
if [[ ! -z $__AUTHOR ]]; then |
|
163 |
printf '%s\n' "author: ${__AUTHOR}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt" |
|
164 |
fi |
|
165 |
|
|
166 |
printf '%s' "$__PANDOC_HEADER" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt" |
|
167 |
|
|
168 |
printf '%s' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt" |
|
169 |
fi |
|
170 |
|
|
171 |
# Now we use pandoc to do to convert it to a PDF. |
|
172 |
echo "Generating PDF" |
|
173 |
pandoc --pdf-engine=xelatex "$__CWD/tmp/text.txt" -o "$__CWD/tmp/text.pdf" |
|
174 |
echo "Cropping PDF" |
|
175 |
pdfcrop --margins '10 5 10 5' "$__CWD/tmp/text.pdf" "$__CWD/tmp/text-croped.pdf" |
|
176 |
|
|
177 |
# Convert it to images |
|
178 |
echo "Converting to images" |
|
179 |
pdftoppm "$__CWD/tmp/text-croped.pdf" "$__CWD/tmp/page" -png -rx $__DPI -ry $__DPI -gray |
|
180 |
|
|
181 |
# convert make the colour space greyscale and the append to each other |
|
182 |
convert -append -colorspace gray +matte -depth 8 "$__CWD/tmp/page-*.png" "$__CWD/tmp/big-page.png" |
|
183 |
|
|
184 |
FINAL_IMAGE="" |
|
185 |
|
|
186 |
# If we invert the final image this is where we do it. |
|
187 |
if [[ $__INVERT_COLOURS == true ]] |
|
188 |
then |
|
189 |
echo "Inverting colours" |
|
190 |
convert "$__CWD/tmp/big-page.png" -channel RGB -negate "$__CWD/tmp/big-page-inverted.png" |
|
191 |
FINAL_IMAGE="$__CWD/tmp/big-page-inverted.png" |
|
192 |
else |
|
193 |
FINAL_IMAGE="$__CWD/tmp/big-page.png" |
|
194 |
fi |
|
195 |
|
|
196 |
echo "Copying final image to $__CWD/$__OUT_FILE.png" |
|
197 |
cp "$FINAL_IMAGE" "$__CWD/$__OUT_FILE.png" |
|
198 |
|
|
199 |
#### |
|
200 |
# Cleanup of eveything. |
|
201 |
#### |
|
202 |
if [[ $__PERSERVE_TMP == true ]] |
|
203 |
then |
|
204 |
echo "Note: Not cleaning up!" |
|
205 |
else |
|
206 |
rm -r "$__CWD/tmp" |
|
207 |
fi |
|
208 |
echo "Done." |
|
209 |
echo "" |
|
210 |
}
|
|
211 |
||
212 |
||
213 |
function __parse_args () { |
|
214 |
if [[ -z "$1" ]] |
|
215 |
then |
|
216 |
echo "Try --help or -h." |
|
217 |
exit 1 |
|
218 |
fi |
|
219 |
|
|
220 |
while [[ $# -gt 0 ]] |
|
221 |
do |
|
222 |
case $1 in |
|
223 |
-i|--input) |
|
224 |
__IN_FILE="$2" |
|
225 |
shift |
|
226 |
shift |
|
227 |
;; |
|
228 |
-o|--output) |
|
229 |
__OUT_FILE="$2" |
|
230 |
shift |
|
231 |
shift |
|
232 |
;; |
|
233 |
-t|--title) |
|
234 |
__TITLE="$2" |
|
235 |
shift |
|
236 |
shift |
|
237 |
;; |
|
238 |
-a|--author) |
|
239 |
__AUTHOR="$2" |
|
240 |
shift |
|
241 |
shift |
|
242 |
;; |
|
243 |
-d|--dpi) |
|
244 |
__DPI="$2" |
|
245 |
shift |
|
246 |
shift |
|
247 |
;; |
|
248 |
-p|--perserve) |
|
249 |
__PERSERVE_TMP=true |
|
250 |
shift |
|
251 |
;; |
|
252 |
--invert) |
|
253 |
__INVERT_COLOURS=true |
|
254 |
shift |
|
255 |
;; |
|
256 |
--no-header) |
|
257 |
__NO_PANDOC_HEADER=true |
|
258 |
shift |
|
259 |
;; |
|
260 |
-h|--help) |
|
261 |
__usage
|
|
262 |
exit |
|
263 |
shift |
|
264 |
;; |
|
265 |
*) |
|
266 |
echo "Unkown argument \"${1}\"." |
|
267 |
exit 1 |
|
268 |
shift |
|
269 |
;; |
|
270 |
--) |
|
271 |
shift |
|
272 |
break |
|
273 |
;; |
|
274 |
esac |
|
275 |
done |
|
276 |
}
|
|
277 |
||
278 |
__main "${@}" |
|
279 |