/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 14:25:13 UTC
  • Revision ID: git-v1:55fc68b13933a25315ad96a4c69e64d962d2c2d4
Fixed install script's --target flag.

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