/useful/trunk-1

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