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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
#!/usr/bin/env bash
####
# FILE NAME process_text_to_image.sh
#
# Changes
#
# 2018-09-03:
# * added --no-header for when you want to use your own pandoc header
#
# 2018-09-22
# * Fixed --no-header...
# Seemed to have forgotten the "$" infront of the variable.
#
# 2021-01-13
# * fixed up the if statments.
#
####
__DPI=300
__IN_FILE=
__OUT_FILE=big_image
__PERSERVE_TMP=false
__INVERT_COLOURS=false
__NO__PANDOC_HEADER=false
__CWD=$PWD
__PANDOC_HEADER="
geometry: margin=0.5cm
papersize: a5
mainfont: DejaVu Serif
fontsize: 12pt
"
__TITLE=""
echo "-----"
echo "---"
echo "--"
echo "-"
function __usage () {
echo "process_text_to_image.sh - Takes one text file and convernts it to a single"
echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop"
echo ""
echo "!IMPORTANT! The folder \"./tmp/\" in the current working directory will be"
echo " used as a temporary storage, and may be deleted, along with it's"
echo " contents!"
echo ""
echo "---------------------"
echo ""
echo "-h --help"
echo " Print this help message"
echo ""
echo "-i <file> --input <file>"
echo " The file to use to convert to an image. "
echo ""
echo "-o <file> --output <file>"
echo " The image to output to. (Default=big_image.png)"
echo ""
echo "-d <integer> -- dpi <integer>"
echo " Set the dpi of the intermediate image relative to an a5 paper."
echo " (Default=300)"
echo ""
echo "-p --perserve"
echo " Do not delete the TMP folder."
echo ""
echo "-invert"
echo " Invert the colours of the final image."
echo ""
echo "-t \"name\" --title \"name\""
echo " Set the title on the the title page."
echo ""
echo "--no-header"
echo " Do not insert the pandoc header. (Default:"
echo "$__PANDOC_HEADER"
echo ")"
echo ""
echo "---------------------"
echo ""
echo "If you are getting an error from convert that the height or width exeeds"
echo "some value, you may want to check the ImageMagick policy.xml file."
echo ""
echo "The path to ImageMagick policy file is:"
convert -list policy | grep .xml
echo ""
echo "---------------------"
}
function __main () {
#ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
echo "__IN_FILE\: $__IN_FILE"
echo "__OUT_FILE\: $__OUT_FILE"
echo "CWD\: $__CWD"
echo "__DPI: $__DPI"
#echo "ESCAPED_CWD\: $ESCAPED_CWD"
if [[ ! -e "$__CWD/$__IN_FILE" ]]
then
echo "!!!in file does not exist!!!"
echo ""
#print_help
exit 1
fi
# first we create a temp folder.
mkdir -p "$__CWD/tmp"
#next we want to copy our file into it.
cp "$__CWD/$__IN_FILE" "$__CWD/tmp/text.txt"
cd "$__CWD/tmp"
# Now we can start the work for this.
if [[ $__NO__PANDOC_HEADER == false ]]
then
# We add a special header to the file to make it pandoc know what to do.
printf '%s\n' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
if [ -n __TITLE="" ]
then
printf '%s\n' "title: ${__TITLE}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
fi
printf '%s' "$__PANDOC_HEADER" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
printf '%s' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
fi
# Now we use pandoc to do to convert it to a PDF.
pandoc --pdf-engine=xelatex "$__CWD/tmp/text.txt" -o "$__CWD/tmp/text.pdf"
pdfcrop --margins '10 5 10 5' "$__CWD/tmp/text.pdf" "$__CWD/tmp/text-croped.pdf"
# Convert it to images
pdftoppm "$__CWD/tmp/text-croped.pdf" "$__CWD/tmp/page" -png -rx $__DPI -ry $__DPI -gray
# convert make the colour space greyscale and the append to each other
convert -append -colorspace gray +matte -depth 8 "$__CWD/tmp/page-*.png" "$__CWD/tmp/big-page.png"
FINAL_IMAGE=""
# If we invert the final image this is where we do it.
if [[ $__INVERT_COLOURS == true ]]
then
convert "$__CWD/tmp/big-page.png" -channel RGB -negate "$__CWD/tmp/big-page-inverted.png"
FINAL_IMAGE="$__CWD/tmp/big-page-inverted.png"
else
FINAL_IMAGE="$__CWD/tmp/big-page.png"
fi
cp "$FINAL_IMAGE" "$__CWD/$__OUT_FILE.png"
####
# Cleanup of eveything.
####
if [[ $__PERSERVE_TMP == true ]]
then
echo "Not cleaning up!"
else
rm -r "$__CWD/tmp"
fi
}
function __parse_args () {
if [[ -z "$1" ]]
then
echo "Try --help or -h."
exit 1
fi
while [[ $# -gt 0 ]]
do
case "${1}" in
-i|--input)
__IN_FILE="$2"
shift
shift
;;
-o|--output)
__OUT_FILE="$2"
shift
shift
;;
-t|--title)
__TITLE="$2"
shift
shift
;;
-d|--dpi)
__DPI="$2"
shift
shift
;;
-p|--perserve)
__PERSERVE_TMP=true
shift
;;
--invert)
__INVERT_COLOURS=true
shift
;;
--no-header)
__NO__PANDOC_HEADER=true
shift
;;
-h|--help)
__usage
exit
shift
;;
*)
#print_help
#exit 1
shift
;;
--)
shift
break
;;
esac
done
}
__parse_args "${@}"
__main
echo "-"
echo "--"
echo "---"
echo "----"
|