Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- import os
- import sys
- import qrcode
- '''
- Converts a file to a pdf filled with QR codes.
- Depends on:
- - qrcode
- - pillow
- - reportlab
- '''
- # pdf generation
- from reportlab.pdfgen import canvas
- from reportlab.lib.units import inch, cm
- from reportlab.lib.pagesizes import A4
- redundancy = qrcode.constants.ERROR_CORRECT_L
- tmp_name = '/tmp/qr.jpg'
- def to_qr(data):
- qr = qrcode.QRCode(
- version=1,
- error_correction=redundancy,
- box_size=4,
- border=4,
- )
- qr.add_data(data)
- qr.make(fit=True)
- return qr.make_image()
- def to_page(c, images):
- assert len(images) <= 6
- images = iter(images)
- for row in range(3):
- for col in range(2):
- try:
- img = images.next()
- img.save(tmp_name)
- x = 10.5 * col + 0.25
- y = 19.5 - (9.5 * row)
- print row, col, x, y
- c.drawImage(tmp_name, x * cm, y * cm, 10*cm, 10*cm)
- except StopIteration:
- return
- # main program
- if __name__ == '__main__':
- if len(sys.argv) != 3:
- print 'usage:', sys.argv[0], '[input file]', '[output pdf]'
- exit(-1)
- _, fin, fout = sys.argv
- # open output
- c = canvas.Canvas(fout, pagesize=A4)
- with open(fin, 'r') as f:
- while 1:
- # read chunks for this page
- chunks = []
- for _ in range(6):
- data = f.read(512)
- if len(data) > 0:
- print 'store:', data.encode('hex')
- chunks.append(data)
- if len(data) < 512:
- break
- # write qr to page
- if len(chunks) > 0:
- chunks = map(to_qr, chunks)
- to_page(c, chunks)
- c.showPage()
- if len(chunks) < 6:
- break
- c.save()
- print 'done'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement