summaryrefslogtreecommitdiff
path: root/guides/applications/imagemagick.org
blob: 9d56cbff5ac9f04b4d1cfef43a9b8a31a978ea54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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