/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-14 11:47:07 UTC
  • Revision ID: git-v1:8ab298a55bc6629f1bf4fc1660469a59f4ebf0cf
General fixup.

* Removed unused ARGS declarations.

* Made all variabels start with two underlines

* Expilitly declared functions as such.

* Expilitly declared strings (file names and such) as strings
  at the start of the files.

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:
13
14
# 2021-01-13
14
15
#   * fixed up the if statments.
15
16
#
16
 
# ------
17
 
 
18
 
ARGS=()
19
 
ARGS="${@}"
20
 
 
21
 
DPI=300
22
 
 
23
 
IN_FILE=
24
 
OUT_FILE=big_image
25
 
PERSERVE_TMP=false
26
 
INVERT_COLOURS=false
27
 
_NO_PANDOC_HEADER=false
28
 
 
29
 
CWD=$PWD
30
 
 
31
 
_PANDOC_HEADER="
 
17
####
 
18
 
 
19
 
 
20
__DPI=300
 
21
 
 
22
__IN_FILE=
 
23
__OUT_FILE=big_image
 
24
__PERSERVE_TMP=false
 
25
__INVERT_COLOURS=false
 
26
__NO__PANDOC_HEADER=false
 
27
 
 
28
__CWD=$PWD
 
29
 
 
30
__PANDOC_HEADER="
32
31
geometry: margin=0.5cm
33
32
papersize: a5
34
33
mainfont: DejaVu Serif
35
34
fontsize: 12pt
36
35
"
37
36
 
38
 
TITLE=""
 
37
__TITLE=""
39
38
 
40
39
echo "-----"
41
40
echo "---"
42
41
echo "--"
43
42
echo "-"
44
43
 
45
 
print_help() {
 
44
function __usage () {
46
45
  
47
46
  echo "process_text_to_image.sh - Takes one text file and convernts it to a single"
48
47
  echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop"
62
61
  echo "-o <file>       --output <file>"
63
62
  echo "        The image to output to. (Default=big_image.png)"
64
63
  echo ""
65
 
  echo "-d <integer>    --dpi <integer>"
 
64
  echo "-d <integer>    -- dpi <integer>"
66
65
  echo "        Set the dpi of the intermediate image relative to an a5 paper."
67
66
  echo "        (Default=300)"
68
67
  echo ""
77
76
  echo ""
78
77
  echo "--no-header"
79
78
  echo "        Do not insert the pandoc header. (Default:"
80
 
  echo "$_PANDOC_HEADER"
 
79
  echo "$__PANDOC_HEADER"
81
80
  echo ")"
82
81
  echo ""
83
82
  echo "---------------------"
91
90
  echo "---------------------"
92
91
}
93
92
 
94
 
 
95
 
 
96
 
 
97
 
 
98
 
main() {
 
93
function __main () {
99
94
  #ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
100
95
  
101
 
  echo "IN_FILE\: $IN_FILE"
102
 
  echo "OUT_FILE\: $OUT_FILE"
103
 
  echo "CWD\: $CWD"
104
 
  echo "DPI: $DPI"
 
96
  echo "__IN_FILE\: $__IN_FILE"
 
97
  echo "__OUT_FILE\: $__OUT_FILE"
 
98
  echo "CWD\: $__CWD"
 
99
  echo "__DPI: $__DPI"
105
100
  #echo "ESCAPED_CWD\: $ESCAPED_CWD"
106
101
  
107
 
  if [[ ! -e "$CWD/$IN_FILE" ]]
 
102
  if [[ ! -e "$__CWD/$__IN_FILE" ]]
108
103
  then
109
104
    echo "!!!in file does not exist!!!"
110
105
    echo ""
113
108
  fi
114
109
  
115
110
  # first we create a temp folder.
116
 
  mkdir -p "$CWD/tmp"
 
111
  mkdir -p "$__CWD/tmp"
117
112
  
118
113
  #next we want to copy our file into it.
119
 
  cp "$CWD/$IN_FILE" "$CWD/tmp/text.txt"
120
 
  cd "$CWD/tmp"
 
114
  cp "$__CWD/$__IN_FILE" "$__CWD/tmp/text.txt"
 
115
  cd "$__CWD/tmp"
121
116
  
122
117
  # Now we can start the work for this.
123
 
  if [[ $_NO_PANDOC_HEADER == false ]]
 
118
  if [[ $__NO__PANDOC_HEADER == false ]]
124
119
  then
125
120
    # We add a special header to the file to make it pandoc know what to do.
126
121
    
127
 
    printf '%s\n' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
128
 
    if [ -n TITLE="" ]
 
122
    printf '%s\n' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
 
123
    if [ -n __TITLE="" ]
129
124
    then
130
 
      printf '%s\n' "title: ${TITLE}" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
 
125
      printf '%s\n' "title: ${__TITLE}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
131
126
    fi
132
127
    
133
 
    printf '%s' "$_PANDOC_HEADER" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
 
128
    printf '%s' "$__PANDOC_HEADER" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
134
129
    
135
 
    printf '%s' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
 
130
    printf '%s' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
136
131
  fi
137
132
  
138
133
  # Now we use pandoc to do to convert it to a PDF.
139
 
  pandoc --pdf-engine=xelatex "$CWD/tmp/text.txt" -o "$CWD/tmp/text.pdf"
 
134
  pandoc --pdf-engine=xelatex "$__CWD/tmp/text.txt" -o "$__CWD/tmp/text.pdf"
140
135
  
141
 
  pdfcrop --margins '10 5 10 5' "$CWD/tmp/text.pdf" "$CWD/tmp/text-croped.pdf"
 
136
  pdfcrop --margins '10 5 10 5' "$__CWD/tmp/text.pdf" "$__CWD/tmp/text-croped.pdf"
142
137
  
143
138
  # Convert it to images
144
 
  pdftoppm "$CWD/tmp/text-croped.pdf" "$CWD/tmp/page" -png -rx $DPI -ry $DPI -gray
 
139
  pdftoppm "$__CWD/tmp/text-croped.pdf" "$__CWD/tmp/page" -png -rx $__DPI -ry $__DPI -gray
145
140
  
146
141
  # convert make the colour space greyscale and the append to each other
147
 
  convert -append -colorspace gray +matte -depth 8 "$CWD/tmp/page-*.png" "$CWD/tmp/big-page.png"
 
142
  convert -append -colorspace gray +matte -depth 8 "$__CWD/tmp/page-*.png" "$__CWD/tmp/big-page.png"
148
143
  
149
144
  FINAL_IMAGE=""
150
145
  
151
146
  # If we invert the final image this is where we do it.
152
 
  if [[ $INVERT_COLOURS == true ]]
153
 
  then
154
 
    convert "$CWD/tmp/big-page.png" -channel RGB -negate "$CWD/tmp/big-page-inverted.png"
155
 
    FINAL_IMAGE="$CWD/tmp/big-page-inverted.png"
156
 
  else
157
 
    FINAL_IMAGE="$CWD/tmp/big-page.png"
158
 
  fi
159
 
  
160
 
  cp "$FINAL_IMAGE" "$CWD/$OUT_FILE.png"
 
147
  if [[ $__INVERT_COLOURS == true ]]
 
148
  then
 
149
    convert "$__CWD/tmp/big-page.png" -channel RGB -negate "$__CWD/tmp/big-page-inverted.png"
 
150
    FINAL_IMAGE="$__CWD/tmp/big-page-inverted.png"
 
151
  else
 
152
    FINAL_IMAGE="$__CWD/tmp/big-page.png"
 
153
  fi
 
154
  
 
155
  cp "$FINAL_IMAGE" "$__CWD/$__OUT_FILE.png"
 
156
  
 
157
  ####
 
158
  # Cleanup of eveything.
 
159
  ####
 
160
  if [[ $__PERSERVE_TMP == true ]]
 
161
  then
 
162
    echo "Not cleaning up!"
 
163
  else
 
164
    rm -r "$__CWD/tmp"
 
165
  fi
161
166
}
162
167
 
163
168
 
164
 
parse_args() {
 
169
function __parse_args () {
165
170
  if [[ -z "$1" ]]
166
171
  then
167
172
    echo "Try --help or -h."
174
179
    
175
180
    case "${1}" in
176
181
      -i|--input)
177
 
      IN_FILE="$2"
 
182
      __IN_FILE="$2"
178
183
      shift
179
184
      shift
180
185
      ;;
181
186
      -o|--output)
182
 
      OUT_FILE="$2"
 
187
      __OUT_FILE="$2"
183
188
      shift
184
189
      shift
185
190
      ;;
186
191
      -t|--title)
187
 
      TITLE="$2"
 
192
      __TITLE="$2"
188
193
      shift
189
194
      shift
190
195
      ;;
191
196
      -d|--dpi)
192
 
      DPI="$2"
 
197
      __DPI="$2"
193
198
      shift
194
199
      shift
195
200
      ;;
196
201
      -p|--perserve)
197
 
      PERSERVE_TMP=true
 
202
      __PERSERVE_TMP=true
198
203
      shift
199
204
      ;;
200
205
      --invert)
201
 
      INVERT_COLOURS=true
 
206
      __INVERT_COLOURS=true
202
207
      shift
203
208
      ;;
204
209
      --no-header)
205
 
      _NO_PANDOC_HEADER=true
 
210
      __NO__PANDOC_HEADER=true
206
211
      shift
207
212
      ;;
208
213
      -h|--help)
209
 
      print_help
 
214
      __usage
210
215
      exit
211
216
      shift
212
217
      ;;
223
228
  done
224
229
}
225
230
 
226
 
parse_args "${@}"
227
 
main
228
 
 
229
 
if [[ $PERSERVE_TMP = true ]]
230
 
then
231
 
  echo "Not cleaning up!"
232
 
else
233
 
  rm -r "$CWD/tmp"
234
 
fi
 
231
__parse_args "${@}"
 
232
__main
235
233
 
236
234
echo "-"
237
235
echo "--"
238
236
echo "---"
239
237
echo "----"
240
 
 
241
 
 
242