/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-09-30 19:22:26 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20240930192226-1pwn3bnp0c09g2za
* fixed this... Finally.

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