Friday, August 14, 2015

Montage of a series of document scans

Assume that we have 22 scanned docs called giro-n.jpg (portrait). This bash code will join two by two documents in one image (landscape), rescale the image to letter size and rotate 90deg in order to put them back in portrait:

c=1; 
for q in `seq 1 11`; 
  do 
     echo $q;   
     montage giros-${c}.jpg  giros-`echo $c + 1| bc`.jpg \
      -geometry 2544x3508 - | convert - -resize 3508x2544 \
      -rotate 90 join/`printf %02d $q`.jpg ; 
     let c=$c+2; 
  done;

The processed images will be in the join/ directory. Then, in order to create a sinlge pdf with all the images, use this python script:

#!/usr/bin/env python
 
import sys
import os
import glob
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter, A4
 
 
if len(sys.argv) < 1:
        print "no args"
        print "usage: this_script outfile.pdf"
        exit
 
 
outpdf=sys.argv[1]
w, h = letter
c = canvas.Canvas(outpdf, pagesize=letter)
 
for filename in sorted(glob.glob("*jpg")):
 s = c.drawImage( filename, 0,0, width=w, height=h, mask=None) 
 print "[", filename,"]"
 c.showPage()
 
c.save()

No comments: