home · contact · privacy
Add make_pbm_pdf.sh.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 5 Mar 2023 22:54:46 +0000 (23:54 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 5 Mar 2023 22:54:46 +0000 (23:54 +0100)
make_pbm_pdf.sh [new file with mode: 0755]

diff --git a/make_pbm_pdf.sh b/make_pbm_pdf.sh
new file mode 100755 (executable)
index 0000000..2242921
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+# Extract PBMs from PDF and create new PDF from them.
+set -e
+n_pages=$(pdfinfo "$1" | grep -e '^Pages:' | awk '{ print $NF }')
+i=1
+while [ "$i" -le "${n_pages}" ]
+do
+    i_formatted=$( printf "%010d" $i)
+    prefix="pdfimages_export_${i_formatted}"
+    echo "extracting page $i as ${prefix}"
+    pdfimages "$1" -l $i -f $i "${prefix}"
+    rm *.ppm
+    source=$(ls -1 "${prefix}"* | head -1)
+    # negate to invert colours, rotate to, well, rotate
+    convert "${source}" -negate -rotate 270 "pdfimages_export_converted_${i_formatted}".pdf
+    rm *.pbm
+    i=$((i + 1))
+done
+pdfunite "pdfimages_export_converted_"* "converted_$1"
+rm pdfimages_export*