ArchWiki’s page on SANE is a good resource.
Scanning on Nix
So I’m scanning from a network-connected scanner (a Brother scanner).
The default GUI I use to scan is GNOME’s Document Scanner.
But you may want to use the scanner from command line.
For this, use the nixpkg sane-backends
which provides scanimage
.
To get a list of devices, use:
scanimage -L
And you’ll get something like:
device `v4l:/dev/video0' is a Noname Integrated Camera: Integrated C virtual device device `escl:http://192.168.1.XX:80' is a Brother XXXX series adf,platen scanner
Here’s how to scan to a file named test1.png:
scanimage --device "escl:http://192.168.1.XX:80" --format=png --resolution=300dpi --mode=color --output-file=test1.png --progress
Here’s how you’d save it as a WebP in one step:
scanimage --device "escl:http://192.168.1.XX:80" --format=png --resolution=300dpi --mode=color |
ffmpeg -hide_banner -f image2pipe -c:v png -i - -f webp -quality 90 -preset text -compression 6 -y "output.webp"
Get scanner options
How to get scanner options for a device:
scanimage --device "escl:http://192.168.1.XX:80" -A
Scan an OCR’d PDF
This will output a PDF named output.pdf.
scanimage --device "escl:http://192.168.1.XX:80" --format=png --resolution=300dpi --mode=gray |
ffmpeg -hide_banner -f image2pipe -c:v png -i - -f image2pipe -c:v mjpeg -q:v 13 - |
tesseract - stdout --oem 1 -l eng --dpi 300 pdf >output.pdf
Here I’m using FFMpeg to convert to a JPEG to reduce the size of the output PDF. Also note that the DPI is set to 300 here. Setting it explicitly prevents tesseract from having to guess, which seems like it can cause the output PDF to be a weirder size.
Scan multiple images into one PDF
Normally you’d just use GNOME’s Document Scanner for this, but you can also use the following bash script:
imgs=()
while
read -p "Continue (y/n)? " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]]
do
scanned=$(mktemp scanned-XXXXXXXX.jpg)
scanimage --device "escl:http://192.168.1.XX:80" --format=png --resolution=300dpi --mode=gray |
ffmpeg -hide_banner -f image2pipe -c:v png -i - -f image2pipe -c:v mjpeg -q:v 13 - >"$scanned"
imgs+=("$scanned")
done
if (( ${#imgs[@]} )); then
read -p "Save as (default output.pdf): " -r
img2pdf "${imgs[@]}" -o "${REPLY:-output.pdf}" && rm "${imgs[@]}"
fi
This relies on sane-backends
, ffmpeg
, and img2pdf
(to convert JPEGs to PDF without quality loss).
Multipage PDF with OCR
And here’s the same thing, but with OCR.
#!/usr/bin/env bash
# Interactively scan a bunch of pages into one OCR’d PDF
# 🄯2023 Josh Mōller-Mara
set -euo pipefail
pages=()
while
read -p "Continue (y/n)? " -n 1 -r
echo
[[ $REPLY =~ ^[Yy]$ ]]
do
scanned=$(mktemp scanned-XXXXXXXX.pdf)
scanimage --device "escl:http://192.168.1.XX:80" --format=png --resolution=300dpi --mode=gray |
ffmpeg -hide_banner -f image2pipe -c:v png -i - -f image2pipe -c:v mjpeg -q:v 13 - |
tesseract - stdout --oem 1 -l eng --dpi 300 pdf >"$scanned"
pages+=("$scanned")
done
if (( ${#pages[@]} )); then
read -p "Save as (default output.pdf): " -r
pdftk "${pages[@]}" cat output "${REPLY:-output.pdf}" && rm "${pages[@]}"
fi
Brother scanner issues
I’m currently using a Brother MFC-L2710DW to scan.
Which looks like it can use either the brscan4
or brscan5
drivers.
Unfortunately this is non-free software that’s just packaged as a .deb
or .rpm
and requires agreeing to some EULA.
Normal scanning works mostly fine without any proprietary drivers.
However, I think this means that I can’t adjust brightness, since I get the following error if I try to set --brightness=100
:
scanimage: attempted to set inactive option brightness
It maybe seems like historically brscan
may have released source code, but no longer does?
I’m not sure why they changed.
To-dos
- See a blog post on compressing hand-written notes linked in my notes on OCR
- Figure out how to automatically threshold and maybe crop receipts