/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-21 15:52:28 UTC
  • mfrom: (22.1.1)
  • Revision ID: git-v1:924d562846d864ade91a0ba932977a58e380ca6f
Merge.

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