Created
January 23, 2017 16:43
-
-
Save rot256/316e7500823ee5566dac9e99238dce61 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment