From ba0ef2a8e8f24bfe270ba29a90782ffa02df615e Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Mon, 14 Mar 2022 19:13:00 +0100 Subject: Add imagemagick script to add watermarks --- guides/imagemagick.org | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/guides/imagemagick.org b/guides/imagemagick.org index 25859e8..9d56cbf 100644 --- a/guides/imagemagick.org +++ b/guides/imagemagick.org @@ -13,3 +13,69 @@ convert '(' a.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 -- cgit v1.2.3