Skip to content

Instantly share code, notes, and snippets.

@baojie
Last active April 27, 2023 03:57
Show Gist options
  • Save baojie/5980346 to your computer and use it in GitHub Desktop.
Save baojie/5980346 to your computer and use it in GitHub Desktop.
Double-sided scanning in linux and converting to pdf. Dependency: sudo apt-get install xsane imagemagick
#!/bin/bash
scanimage --batch --batch-double
read -p "Flip papers in the feeder, last page on top. Press [Enter] key to start..."
endpage=$(echo "$(ls *.pnm -1 | wc -l) * 2" | bc)
scanimage --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
@ChenSun-Phys
Copy link

Awesome. The only nitpick is that if there are other jpg's in my current folder it will combine all of them into the final pdf. Also, it will rm all my preexisting pnm files. I've added a few small things

  • temporary folder to make sure the work dir is clean.
  • choose feeder from ADF instead of flatbed.
  • manually set resolution and mode
#!/bin/bash
mkdir scan_tmp
cd scan_tmp
scanimage --batch --batch-double -d 'escl:http://192.168.100.142:80' --mode lineart  --resolution 300 --source ADF
read -p "Flip papers in the feeder, last page on top. Press [Enter] key to start..."
endpage=$(echo "$(ls *.pnm -1 | wc -l) * 2" | bc)
scanimage --batch --batch-increment -2 --batch-start $endpage -d 'escl:http://192.168.100.142:80' --mode lineart  --resolution 300 --source ADF
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
cd ..
mv scan_tmp/output.pdf ./
rm -rf scan_tmp/

Lastly, my convert does not convert PDF by throwing out the following error:

convert-im6.q16: attempt to perform an operation not allowed by the security policy `PDF'

I followed the solution here to comment out <policy domain="coder" rights="none" pattern="PDF" /> inside /etc/ImageMagick-6/policy.xml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment