summaryrefslogtreecommitdiff
path: root/guides/applications/imagemagick.org
diff options
context:
space:
mode:
authorKévin Le Gouguec <kevin.legouguec@gmail.com>2025-01-14 22:48:35 +0100
committerKévin Le Gouguec <kevin.legouguec@gmail.com>2025-01-14 23:26:44 +0100
commitcf8a3f15ee1c80b874be10cbdd34496b84560f59 (patch)
treec7a77d6938c6d8d539a6ae1e72160732d440ccf5 /guides/applications/imagemagick.org
parentfaf50994d58d2651a2ab1bb1ed94dce1feb246bd (diff)
downloadmemory-leaks-cf8a3f15ee1c80b874be10cbdd34496b84560f59.tar.xz
Sort guides up a bit
Diffstat (limited to 'guides/applications/imagemagick.org')
-rw-r--r--guides/applications/imagemagick.org81
1 files changed, 81 insertions, 0 deletions
diff --git a/guides/applications/imagemagick.org b/guides/applications/imagemagick.org
new file mode 100644
index 0000000..9d56cbf
--- /dev/null
+++ b/guides/applications/imagemagick.org
@@ -0,0 +1,81 @@
+ImageMagick recipes I should stuff into config files or scripts, but
+haven't yet.
+* Configuration
+** Allow conversion from/to PDF
+- Open ImageMagick's =policy.xml= configuration file.
+- Locate the =<policy>= tag with a =pattern= including "PDF".
+- Comment it out.
+* Comparing images
+~magick compare~ is a thing; ~magick convert~ can also do [[https://stackoverflow.com/a/33673440][neat stuff]]:
+#+begin_src sh
+convert '(' a.png -flatten -grayscale Rec709Luminance ')' \
+ '(' b.png -flatten -grayscale Rec709Luminance ')' \
+ '(' -clone 0-1 -compose darken -composite ')' \
+ -channel RGB -combine diff.png
+#+end_src
+
+* Adding watermarks
+Someday, in the Glorious Future, every thirdparty that insists on
+having a copy of my personal documents will be legally obligated to
+request it from a trusted government service that steganographizes the
+requester's identity in the files they send.
+
+Until then…
+#+begin_src sh
+#!/bin/bash
+
+set -eu
+
+src=$1
+dst=$2
+label=$3
+
+mark ()
+{
+ local marksrc=$1
+ local markdst=$2
+
+ w=$(identify -format '%w\n' "$marksrc" | head -1)
+ h=$(identify -format '%h\n' "$marksrc" | head -1)
+ textw=$((w/3))
+ texth=$((h/8))
+
+ pointsize=$((80*w/2500))
+ offx=$((100*w/2500))
+ offy=$((400*h/3500))
+
+ convert -size ${textw}x${texth} xc:none \
+ -fill '#80808080' -pointsize $pointsize \
+ -annotate 340x340+${offx}+${offy} "$label" \
+ miff:- | \
+ composite -tile - "$marksrc" "$markdst"
+}
+
+if [[ "$src" =~ \.pdf ]]
+then
+ # AFAICT if I feed a multi-page PDF to composite, it outputs only
+ # the first page. Jump through hoops to split and concatenate the
+ # PDF; also, roundtrip through JPG because setting -density (to
+ # preserve a good quality) messes with the size calculations for
+ # the label.
+
+ tmpd=$(mktemp -d)
+ bname=$(basename "$src")
+ qpdf --split-pages "$src" "${tmpd}/$bname"
+
+ shopt -s extglob
+
+ for page in ${tmpd}/${bname/%.pdf/-+([0-9]).pdf}
+ do
+ convert -density 300 -flatten "$page" "${page/.pdf/.jpg}"
+ mark "${page/.pdf/.jpg}" "${page/.pdf/-MARKED.jpg}"
+ convert -density 300 "${page/.pdf/-MARKED.jpg}" "${page/.pdf/-MARKED.pdf}"
+ done
+
+ qpdf --empty --pages ${tmpd}/${bname/%.pdf/-+([0-9])-MARKED.pdf} -- "$dst"
+
+ exit
+fi
+
+mark "$src" "$dst"
+#+end_src