/useful/trunk-1

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