/useful/trunk-1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/useful/trunk-1

« back to all changes in this revision

Viewing changes to scripts/process_text_to_image.sh

  • Committer: Gustav Hartvigsson
  • Date: 2021-01-13 18:44:02 UTC
  • Revision ID: git-v1:0dbf5fcfbb68944b25dbeeeceb02ac414ac330ad
Inital code.

Show diffs side-by-side

added added

removed removed

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