/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 19:03:20 UTC
  • Revision ID: git-v1:5a81e50921d5c2c9c4e9c78d6293500f8de4b319
* pdf2images.sh:
   - Fixed usae message
* process_text_to_image.sh
  - Added sanity check
  - General logic fixes
  - Added --author argument
  - Fixed help message
  - formating.

* wav2mp4
  - minor formating errors
  - added sanity check
  - use /tmp/ instead of current folder for the temporary image

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# 2021-01-13
15
15
#   * fixed up the if statments.
16
16
#
 
17
# 2024-07-10
 
18
#   * Added sanity check
 
19
#   * General logic fixes.
 
20
#   * Added --author argument
 
21
#   * Fixed help message formating
17
22
####
18
23
 
19
 
 
20
24
__DPI=300
21
25
 
22
26
__IN_FILE=
23
27
__OUT_FILE=big_image
24
28
__PERSERVE_TMP=false
25
29
__INVERT_COLOURS=false
26
 
__NO__PANDOC_HEADER=false
 
30
__NO_PANDOC_HEADER=false
27
31
 
28
32
__CWD=$PWD
29
33
 
35
39
"
36
40
 
37
41
__TITLE=""
 
42
__AUTHOR=""
38
43
 
39
 
echo "-----"
40
 
echo "---"
41
 
echo "--"
42
 
echo "-"
 
44
__SANITY=true
43
45
 
44
46
function __usage () {
45
47
  
52
54
  echo ""
53
55
  echo "---------------------"
54
56
  echo ""
55
 
  echo "-h      --help"
 
57
  echo "-h             --help"
56
58
  echo "        Print this help message"
57
59
  echo ""
58
 
  echo "-i <file>       --input <file>"
 
60
  echo "-i <file>      --input <file>"
59
61
  echo "        The file to use to convert to an image. "
60
62
  echo ""
61
 
  echo "-o <file>       --output <file>"
 
63
  echo "-o <file>      --output <file>"
62
64
  echo "        The image to output to. (Default=big_image.png)"
63
65
  echo ""
64
 
  echo "-d <integer>    -- dpi <integer>"
 
66
  echo "-d <integer>   --dpi <integer>"
65
67
  echo "        Set the dpi of the intermediate image relative to an a5 paper."
66
68
  echo "        (Default=300)"
67
69
  echo ""
68
 
  echo "-p              --perserve"
 
70
  echo "-p             --perserve"
69
71
  echo "        Do not delete the TMP folder."
70
72
  echo ""
71
 
  echo "-invert"
 
73
  echo "--invert"
72
74
  echo "        Invert the colours of the final image."
73
75
  echo ""
74
 
  echo "-t \"name\"     --title \"name\""
 
76
  echo "-t \"name\"      --title \"name\""
75
77
  echo "        Set the title on the the title page."
76
78
  echo ""
 
79
  echo "-a \"name\"      --author \"name\""
 
80
  echo "        Set an author to the title page."
 
81
  echo ""
77
82
  echo "--no-header"
78
83
  echo "        Do not insert the pandoc header. (Default:"
79
84
  echo "$__PANDOC_HEADER"
90
95
  echo "---------------------"
91
96
}
92
97
 
 
98
function __silent () {
 
99
  $@ >> /dev/null 2>&1
 
100
  return $?
 
101
}
 
102
 
 
103
function __find_tool () {
 
104
  __silent which $1
 
105
 
 
106
  if [ $? -gt 0 ]; then
 
107
    echo "    Can't find tool \"${1}\"."
 
108
    ___SANITY=false
 
109
  fi
 
110
}
 
111
 
 
112
function __sanity_check () {
 
113
  # Check that we have the tools needed.
 
114
  __find_tool pandoc
 
115
  __find_tool xelatex
 
116
  __find_tool convert
 
117
  __find_tool pdftoppm
 
118
  __find_tool pdfcrop
 
119
 
 
120
  if [[ $___SANITY == true ]]; then
 
121
    echo "Please install the missing tools."
 
122
    echo ""
 
123
    exit 1
 
124
  fi
 
125
}
 
126
 
93
127
function __main () {
94
 
  #ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
 
128
  # FIXME: Split the functionality out of the main function.
 
129
  # FIXME: Use mkdtemp instead of the folder we are in.
 
130
  __parse_args "${@}"
 
131
  __sanity_check
95
132
  
96
133
  echo "__IN_FILE\: $__IN_FILE"
97
134
  echo "__OUT_FILE\: $__OUT_FILE"
98
135
  echo "CWD\: $__CWD"
99
136
  echo "__DPI: $__DPI"
100
 
  #echo "ESCAPED_CWD\: $ESCAPED_CWD"
101
137
  
102
 
  if [[ ! -e "$__CWD/$__IN_FILE" ]]
 
138
  if [[ ! -e "$__CWD/$__IN_FILE" ]] || [[ -z $__IN_FILE  ]]
103
139
  then
104
 
    echo "!!!in file does not exist!!!"
 
140
    echo "The provided <infile> does not exit."
105
141
    echo ""
106
 
    #print_help
107
142
    exit 1
108
143
  fi
109
144
  
115
150
  cd "$__CWD/tmp"
116
151
  
117
152
  # Now we can start the work for this.
118
 
  if [[ $__NO__PANDOC_HEADER == false ]]
 
153
  if [[ $__NO_PANDOC_HEADER == false ]]
119
154
  then
 
155
    # FIXME: This is cursed.
120
156
    # We add a special header to the file to make it pandoc know what to do.
 
157
    #
 
158
    # The header is built from the bottom up. The input text at the bottom, and
 
159
    # the rest of the "elements" added above.
121
160
    
122
161
    printf '%s\n' "---" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
123
 
    if [ -n __TITLE="" ]
124
 
    then
 
162
    if [[ ! -z $__TITLE ]]; then
125
163
      printf '%s\n' "title: ${__TITLE}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
126
164
    fi
 
165
 
 
166
    if [[ ! -z $__AUTHOR ]]; then
 
167
      printf '%s\n' "author: ${__AUTHOR}" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
 
168
    fi
127
169
    
128
170
    printf '%s' "$__PANDOC_HEADER" "$(cat "$__CWD/tmp/text.txt")" > "$__CWD/tmp/text.txt"
129
171
    
131
173
  fi
132
174
  
133
175
  # Now we use pandoc to do to convert it to a PDF.
 
176
  echo "Generating PDF"
134
177
  pandoc --pdf-engine=xelatex "$__CWD/tmp/text.txt" -o "$__CWD/tmp/text.pdf"
135
 
  
 
178
  echo "Cropping PDF"
136
179
  pdfcrop --margins '10 5 10 5' "$__CWD/tmp/text.pdf" "$__CWD/tmp/text-croped.pdf"
137
180
  
138
181
  # Convert it to images
 
182
  echo "Converting to images"
139
183
  pdftoppm "$__CWD/tmp/text-croped.pdf" "$__CWD/tmp/page" -png -rx $__DPI -ry $__DPI -gray
140
184
  
141
185
  # convert make the colour space greyscale and the append to each other
146
190
  # If we invert the final image this is where we do it.
147
191
  if [[ $__INVERT_COLOURS == true ]]
148
192
  then
 
193
    echo "Inverting colours"
149
194
    convert "$__CWD/tmp/big-page.png" -channel RGB -negate "$__CWD/tmp/big-page-inverted.png"
150
195
    FINAL_IMAGE="$__CWD/tmp/big-page-inverted.png"
151
196
  else
152
197
    FINAL_IMAGE="$__CWD/tmp/big-page.png"
153
198
  fi
154
199
  
 
200
  echo "Copying final image to $__CWD/$__OUT_FILE.png"
155
201
  cp "$FINAL_IMAGE" "$__CWD/$__OUT_FILE.png"
156
202
  
157
203
  ####
159
205
  ####
160
206
  if [[ $__PERSERVE_TMP == true ]]
161
207
  then
162
 
    echo "Not cleaning up!"
 
208
    echo "Note: Not cleaning up!"
163
209
  else
164
210
    rm -r "$__CWD/tmp"
165
211
  fi
 
212
  echo "Done."
 
213
  echo ""
166
214
}
167
215
 
168
216
 
173
221
    exit 1
174
222
  fi
175
223
  
176
 
  
177
224
  while [[ $# -gt 0 ]]
178
225
  do
179
 
    
180
 
    case "${1}" in
 
226
    case $1 in
181
227
      -i|--input)
182
 
      __IN_FILE="$2"
183
 
      shift
184
 
      shift
 
228
        __IN_FILE="$2"
 
229
        shift
 
230
        shift
185
231
      ;;
186
232
      -o|--output)
187
 
      __OUT_FILE="$2"
188
 
      shift
189
 
      shift
 
233
        __OUT_FILE="$2"
 
234
        shift
 
235
        shift
190
236
      ;;
191
237
      -t|--title)
192
 
      __TITLE="$2"
193
 
      shift
194
 
      shift
 
238
        __TITLE="$2"
 
239
        shift
 
240
        shift
 
241
      ;;
 
242
      -a|--author)
 
243
        __AUTHOR="$2"
 
244
        shift
 
245
        shift
195
246
      ;;
196
247
      -d|--dpi)
197
 
      __DPI="$2"
198
 
      shift
199
 
      shift
 
248
        __DPI="$2"
 
249
        shift
 
250
        shift
200
251
      ;;
201
252
      -p|--perserve)
202
 
      __PERSERVE_TMP=true
203
 
      shift
 
253
        __PERSERVE_TMP=true
 
254
        shift
204
255
      ;;
205
256
      --invert)
206
 
      __INVERT_COLOURS=true
207
 
      shift
 
257
        __INVERT_COLOURS=true
 
258
        shift
208
259
      ;;
209
260
      --no-header)
210
 
      __NO__PANDOC_HEADER=true
211
 
      shift
 
261
        __NO_PANDOC_HEADER=true
 
262
        shift
212
263
      ;;
213
264
      -h|--help)
214
 
      __usage
215
 
      exit
216
 
      shift
 
265
         __usage
 
266
         exit
 
267
         shift
217
268
      ;;
218
269
      *)
219
 
      #print_help
220
 
      #exit 1
221
 
      shift
 
270
        echo "Unkown argument \"${1}\"."
 
271
        exit 1
 
272
        shift
222
273
      ;;
223
274
      --)
224
 
      shift
225
 
      break
 
275
        shift
 
276
        break
226
277
      ;;
227
278
    esac
228
279
  done
229
280
}
230
281
 
231
 
__parse_args "${@}"
232
 
__main
 
282
__main "${@}"
233
283
 
234
 
echo "-"
235
 
echo "--"
236
 
echo "---"
237
 
echo "----"