/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 18:44:02 UTC
  • Revision ID: git-v1:0dbf5fcfbb68944b25dbeeeceb02ac414ac330ad
Inital code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env bash
 
2
 
 
3
# ------
 
4
# Changes
 
5
#
 
6
# 2018-09-03:
 
7
# added --no-header for when you want to use your own pandoc header
 
8
#
 
9
# 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="
 
28
geometry: margin=0.5cm
 
29
papersize: a5
 
30
mainfont: DejaVu Serif
 
31
fontsize: 12pt
 
32
"
 
33
 
 
34
TITLE=""
 
35
 
 
36
echo "-----"
 
37
echo "---"
 
38
echo "--"
 
39
echo "-"
 
40
 
 
41
print_help() {
 
42
  
 
43
  echo "process_text_to_image.sh - Takes one text file and convernts it to a single"
 
44
  echo "image using pandoc, xelatex, imagemagick, pdftoppm, pdfcrop"
 
45
  echo ""
 
46
  echo "!IMPORTANT! The folder \"./tmp/\" in the current working directory will be"
 
47
  echo "            used as a temporary storage, and may be deleted, along with it's"
 
48
  echo "            contents!"
 
49
  echo ""
 
50
  echo "---------------------"
 
51
  echo ""
 
52
  echo "-h      --help"
 
53
  echo "        Print this help message"
 
54
  echo ""
 
55
  echo "-i <file>       --input <file>"
 
56
  echo "        The file to use to convert to an image. "
 
57
  echo ""
 
58
  echo "-o <file>       --output <file>"
 
59
  echo "        The image to output to. (Default=big_image.png)"
 
60
  echo ""
 
61
  echo "-d <integer>    --dpi <integer>"
 
62
  echo "        Set the dpi of the intermediate image relative to an a5 paper."
 
63
  echo "        (Default=300)"
 
64
  echo ""
 
65
  echo "-p              --perserve"
 
66
  echo "        Do not delete the TMP folder."
 
67
  echo ""
 
68
  echo "-invert"
 
69
  echo "        Invert the colours of the final image."
 
70
  echo ""
 
71
  echo "-t \"name\"     --title \"name\""
 
72
  echo "        Set the title on the the title page."
 
73
  echo ""
 
74
  echo "--no-header"
 
75
  echo "        Do not insert the pandoc header. (Default:"
 
76
  echo "$_PANDOC_HEADER"
 
77
  echo ")"
 
78
  echo ""
 
79
  echo "---------------------"
 
80
  echo ""
 
81
  echo "If you are getting an error from convert that the height or width exeeds"
 
82
  echo "some value, you may want to check the ImageMagick policy.xml file."
 
83
  echo ""
 
84
  echo "The path to ImageMagick policy file is:"
 
85
  convert -list policy | grep .xml 
 
86
  echo ""
 
87
  echo "---------------------"
 
88
}
 
89
 
 
90
 
 
91
 
 
92
 
 
93
 
 
94
main() {
 
95
  #ESCAPED_CWD=$(echo ${CWD} | sed 's/ /\\ /g' | sed "s/'/\\\'/g" )
 
96
  
 
97
  echo "IN_FILE\: $IN_FILE"
 
98
  echo "OUT_FILE\: $OUT_FILE"
 
99
  echo "CWD\: $CWD"
 
100
  echo "DPI: $DPI"
 
101
  #echo "ESCAPED_CWD\: $ESCAPED_CWD"
 
102
  
 
103
  if [ ! -e "$CWD/$IN_FILE" ]
 
104
  then
 
105
    echo "!!!in file does not exist!!!"
 
106
    echo ""
 
107
    #print_help
 
108
    exit 1
 
109
  fi
 
110
  
 
111
  # first we create a temp folder.
 
112
  mkdir -p "$CWD/tmp"
 
113
  
 
114
  #next we want to copy our file into it.
 
115
  cp "$CWD/$IN_FILE" "$CWD/tmp/text.txt"
 
116
  cd "$CWD/tmp"
 
117
  
 
118
  # Now we can start the work for this.
 
119
  if [ $_NO_PANDOC_HEADER = false ]
 
120
  then
 
121
    # We add a special header to the file to make it pandoc know what to do.
 
122
    
 
123
    printf '%s\n' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
 
124
    if [ -n TITLE="" ]
 
125
    then
 
126
      printf '%s\n' "title: ${TITLE}" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
 
127
    fi
 
128
    
 
129
    printf '%s' "$_PANDOC_HEADER" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
 
130
    
 
131
    printf '%s' "---" "$(cat "$CWD/tmp/text.txt")" > "$CWD/tmp/text.txt"
 
132
  fi
 
133
  
 
134
  # 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"
 
136
  
 
137
  pdfcrop --margins '10 5 10 5' "$CWD/tmp/text.pdf" "$CWD/tmp/text-croped.pdf"
 
138
  
 
139
  # Convert it to images
 
140
  pdftoppm "$CWD/tmp/text-croped.pdf" "$CWD/tmp/page" -png -rx $DPI -ry $DPI -gray
 
141
  
 
142
  # 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"
 
144
  
 
145
  FINAL_IMAGE=""
 
146
  
 
147
  # 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"
 
157
}
 
158
 
 
159
 
 
160
parse_args() {
 
161
  if [ -z "$1" ]
 
162
  then
 
163
    echo "Try --help or -h."
 
164
    exit 1
 
165
  fi
 
166
  
 
167
  
 
168
  while [[ $# -gt 0 ]]
 
169
  do
 
170
    eval key="${1}"
 
171
    
 
172
    case "${1}" in
 
173
      -i|--input)
 
174
      IN_FILE="$2"
 
175
      shift
 
176
      shift
 
177
      ;;
 
178
      -o|--output)
 
179
      OUT_FILE="$2"
 
180
      shift
 
181
      shift
 
182
      ;;
 
183
      -t|--title)
 
184
      TITLE="$2"
 
185
      shift
 
186
      shift
 
187
      ;;
 
188
      -d|--dpi)
 
189
      DPI="$2"
 
190
      shift
 
191
      shift
 
192
      ;;
 
193
      -p|--perserve)
 
194
      PERSERVE_TMP=true
 
195
      shift
 
196
      ;;
 
197
      --invert)
 
198
      INVERT_COLOURS=true
 
199
      shift
 
200
      ;;
 
201
      --no-header)
 
202
      _NO_PANDOC_HEADER=true
 
203
      shift
 
204
      ;;
 
205
      -h|--help)
 
206
      print_help
 
207
      exit
 
208
      shift
 
209
      ;;
 
210
      *)
 
211
      #print_help
 
212
      #exit 1
 
213
      shift
 
214
      ;;
 
215
      --)
 
216
      shift
 
217
      break
 
218
      ;;
 
219
    esac
 
220
  done
 
221
}
 
222
 
 
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
 
232
 
 
233
echo "-"
 
234
echo "--"
 
235
echo "---"
 
236
echo "----"
 
237
 
 
238
 
 
239