Wednesday, June 15, 2022

Linux duplex scanning

I have been using XSane for scanning but it seems not to have an option for duplex batch scanning.

I found this gist. It didn't quite work but I adapted it to work with my scanner:

#!/bin/bash
scanimage -d 'brother4:net1;dev0' --batch --batch-double
read -p "Flip papers in the feeder, last page on top, press Enter..."
endpage=$(echo "$(ls *.pnm -1 | wc -l) * 2" | bc)
scanimage -d 'brother4:net1;dev0' --batch --batch-increment -2 --batch-start $endpage
for file in *.pnm; do convert $file $file.jpg; done
rm *.pnm
convert $(ls *.jpg -1v | paste) -compress jpeg -page A4 output.pdf
rm *.jpg
 

This works, but the scanner device is specific to my setup. To find the scanner I ran: scanimage -L

Otherwise, it would be better if it created a temporary directory, put all the files in it then copied only the output file back to current directory and deleted the temporary directory. But in the meantime, this works and is easier than struggling with the X(In-)Sane UI.

I had to change ImageMagick policy to allow writing the PDF using convert: in /etc/ImageMagick-6/policy.xml I change "none" to "write" in the line:   <policy domain="coder" rights="write" pattern="PDF" />

This allows 'convert' (and presumably other commands of the ImageMagic set) to write PDF files but not to read them. Reading them is a security concern but writing them should be safe enough.

No comments:

Labels