/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 20:29:17 UTC
  • Revision ID: git-v1:4502736d0e4c4af9a28416c4b04bf9725893c5a0
Fixed up the rest of the if statments.

  I have no idea how it even worked before.

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