Skip to content

Instantly share code, notes, and snippets.

@thequux
Created October 28, 2014 14:01
Show Gist options
  • Save thequux/05ee1c972de1616f9f31 to your computer and use it in GitHub Desktop.
Save thequux/05ee1c972de1616f9f31 to your computer and use it in GitHub Desktop.
Make-based scanning frontend
# Usage: make name=<document name> t<arget>
# Start with the init target, then use "scan" to run the scan. Finally, run "pdf" to get a compressed PDF file.
# Parameters that can be set in a Makefile.defs include:
# papersize: Provides defaults for papersize_width and papersize_height; defaults to a4
# papersize_{width,height}: Width and height of page in px
# resolution: resolution of scan in dpi
# rotate: Rotation angle (0,90,180,270). Applied during compression.
name ?= $(error "Read comments at top of makefile")
DIR := $(name)
ofile := $(name)
-include $(name)/Makefile.defs
extradeps := $(shell test -f $(name)/Makefile.defs && echo $(name)/Makefile.defs) Makefile
resolution ?= 150
ifdef papersize
papersize_width ?= $(strip $(shell echo "`paperconf -p $(papersize) -w` * $(resolution) / 72" |bc))
papersize_height ?= $(strip $(shell echo "`paperconf -p $(papersize) -h` * $(resolution) / 72" |bc))
endif
pdf: $(name)/$(ofile).pdf
$(name)/$(ofile).pdf: $(patsubst %.ppm, %.jpg, $(wildcard $(name)/*.ppm))
convert $(sort $+) -density $(resolution) $@
%.jpg: %.ppm $(extradeps)
convert $< -crop $(papersize_width)x$(papersize_height)+0+0 $(if $(rotate),-rotate $(rotate)) $@
init: $(name)/.init
$(name):
test -d $(name) || mkdir -p $(name)
$(name)/.init: $(name)
echo "resolution=$(resolution)" >>$(name)/Makefile.defs
echo "papersize=$(papersize)" >>$(name)/Makefile.defs
echo "$(if $(rotate),,#)rotate=$(rotate)" >>$(name)/Makefile.defs
touch $@
scan: $(name)/.scan
$(name)/.scan: $(name)/.init
scanimage --batch=$(name)/page%04d.ppm -p --batch-prompt --resolution $(resolution)
touch $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment