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 == 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