/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: 2022-03-22 16:38:04 UTC
  • Revision ID: git-v1:5f6cfed1c6fef9c23b0efd88abd3bea1f3a04e4b
* Make gifs work.

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