memory-leaks

Still reachable: lots of words in many pages.
git clone https://git.kevinlegouguec.net/memory-leaks
Log | Files | Refs | README | LICENSE

imagemagick.org (2409B)


      1 ImageMagick recipes I should stuff into config files or scripts, but
      2 haven't yet.
      3 * Configuration
      4 ** Allow conversion from/to PDF
      5 - Open ImageMagick's =policy.xml= configuration file.
      6 - Locate the =<policy>= tag with a =pattern= including "PDF".
      7 - Comment it out.
      8 * Comparing images
      9 ~magick compare~ is a thing; ~magick convert~ can also do [[https://stackoverflow.com/a/33673440][neat stuff]]:
     10 #+begin_src sh
     11 convert '(' a.png -flatten -grayscale Rec709Luminance ')' \
     12         '(' b.png -flatten -grayscale Rec709Luminance ')' \
     13         '(' -clone 0-1 -compose darken -composite ')' \
     14         -channel RGB -combine diff.png
     15 #+end_src
     16 
     17 * Adding watermarks
     18 Someday, in the Glorious Future, every thirdparty that insists on
     19 having a copy of my personal documents will be legally obligated to
     20 request it from a trusted government service that steganographizes the
     21 requester's identity in the files they send.
     22 
     23 Until then…
     24 #+begin_src sh
     25 #!/bin/bash
     26 
     27 set -eu
     28 
     29 src=$1
     30 dst=$2
     31 label=$3
     32 
     33 mark ()
     34 {
     35     local marksrc=$1
     36     local markdst=$2
     37 
     38     w=$(identify -format '%w\n' "$marksrc" | head -1)
     39     h=$(identify -format '%h\n' "$marksrc" | head -1)
     40     textw=$((w/3))
     41     texth=$((h/8))
     42 
     43     pointsize=$((80*w/2500))
     44     offx=$((100*w/2500))
     45     offy=$((400*h/3500))
     46 
     47     convert -size ${textw}x${texth} xc:none                 \
     48             -fill '#80808080' -pointsize $pointsize         \
     49             -annotate 340x340+${offx}+${offy} "$label"      \
     50             miff:- |                                        \
     51         composite -tile - "$marksrc" "$markdst"
     52 }
     53 
     54 if [[ "$src" =~ \.pdf ]]
     55 then
     56     # AFAICT if I feed a multi-page PDF to composite, it outputs only
     57     # the first page.  Jump through hoops to split and concatenate the
     58     # PDF; also, roundtrip through JPG because setting -density (to
     59     # preserve a good quality) messes with the size calculations for
     60     # the label.
     61 
     62     tmpd=$(mktemp -d)
     63     bname=$(basename "$src")
     64     qpdf --split-pages "$src" "${tmpd}/$bname"
     65 
     66     shopt -s extglob
     67 
     68     for page in ${tmpd}/${bname/%.pdf/-+([0-9]).pdf}
     69     do
     70         convert -density 300 -flatten "$page" "${page/.pdf/.jpg}"
     71         mark "${page/.pdf/.jpg}" "${page/.pdf/-MARKED.jpg}"
     72         convert -density 300 "${page/.pdf/-MARKED.jpg}" "${page/.pdf/-MARKED.pdf}"
     73     done
     74 
     75     qpdf --empty --pages ${tmpd}/${bname/%.pdf/-+([0-9])-MARKED.pdf} -- "$dst"
     76 
     77     exit
     78 fi
     79 
     80 mark "$src" "$dst"
     81 #+end_src