49 lines
1.4 KiB
Nix
49 lines
1.4 KiB
Nix
{ pkgs ? import <nixpkgs> {} }:
|
|
let
|
|
crop_ppm = (pkgs.writers.writePython3Bin "crop_ppm" {libraries = [pkgs.python3Packages.opencv4];} ''
|
|
import cv2
|
|
import sys
|
|
|
|
for path in sys.argv[1:]:
|
|
img = cv2.imread(path)[30:-30, 30:]
|
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
_, thresh = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)
|
|
contours, hierarchy = cv2.findContours(
|
|
thresh,
|
|
cv2.RETR_EXTERNAL,
|
|
cv2.CHAIN_APPROX_SIMPLE
|
|
)
|
|
cnt = max(contours, key=cv2.contourArea)
|
|
x, y, w, h = cv2.boundingRect(cnt)
|
|
print(path, x, y, w, h)
|
|
crop = img[y:y + h, x:x + w]
|
|
cv2.imwrite(path, crop)
|
|
'');
|
|
in
|
|
pkgs.mkShell {
|
|
packages = [
|
|
pkgs.poppler_utils
|
|
pkgs.mupdf
|
|
crop_ppm
|
|
(pkgs.writers.writeDashBin "generate" ''
|
|
set -x
|
|
pwd=$(pwd)
|
|
tmpdir=$(mktemp -d)
|
|
cd "$tmpdir"
|
|
[ $# -eq 1 ] || {
|
|
echo Please provide one PDF file as argument. >&2
|
|
exit 1
|
|
}
|
|
[ -f "$1" ] || {
|
|
echo "$1" does not exist. >&2
|
|
exit 1
|
|
}
|
|
basename=$(basename "$1" .pdf)
|
|
${pkgs.poppler_utils}/bin/pdftoppm "$1" exploded
|
|
${crop_ppm}/bin/crop_ppm exploded*.ppm
|
|
${pkgs.imagemagick}/bin/convert exploded*.ppm "$pwd/cropped-$basename.pdf"
|
|
${pkgs.mupdf}/bin/mutool poster -x 2 "$pwd/cropped-$basename.pdf" "$pwd/split-cropped-$basename.pdf"
|
|
'')
|
|
];
|
|
}
|