/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: 2024-07-10 20:46:40 UTC
  • Revision ID: git-v1:72a9ba15a007b97bd061bb2f8e4d5959f0c3c34c
* added bash function libary.

Show diffs side-by-side

added added

removed removed

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