Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. import os
  4. import sys
  5. import qrcode
  6.  
  7. '''
  8. Converts a file to a pdf filled with QR codes.
  9. Depends on:
  10. - qrcode
  11. - pillow
  12. - reportlab
  13. '''
  14.  
  15. # pdf generation
  16.  
  17. from reportlab.pdfgen import canvas
  18. from reportlab.lib.units import inch, cm
  19. from reportlab.lib.pagesizes import A4
  20.  
  21. redundancy = qrcode.constants.ERROR_CORRECT_L
  22. tmp_name = '/tmp/qr.jpg'
  23.  
  24. def to_qr(data):
  25. qr = qrcode.QRCode(
  26. version=1,
  27. error_correction=redundancy,
  28. box_size=4,
  29. border=4,
  30. )
  31. qr.add_data(data)
  32. qr.make(fit=True)
  33. return qr.make_image()
  34.  
  35. def to_page(c, images):
  36. assert len(images) <= 6
  37. images = iter(images)
  38. for row in range(3):
  39. for col in range(2):
  40. try:
  41. img = images.next()
  42. img.save(tmp_name)
  43. x = 10.5 * col + 0.25
  44. y = 19.5 - (9.5 * row)
  45. print row, col, x, y
  46. c.drawImage(tmp_name, x * cm, y * cm, 10*cm, 10*cm)
  47. except StopIteration:
  48. return
  49.  
  50. # main program
  51.  
  52. if __name__ == '__main__':
  53. if len(sys.argv) != 3:
  54. print 'usage:', sys.argv[0], '[input file]', '[output pdf]'
  55. exit(-1)
  56. _, fin, fout = sys.argv
  57.  
  58. # open output
  59. c = canvas.Canvas(fout, pagesize=A4)
  60.  
  61. with open(fin, 'r') as f:
  62. while 1:
  63. # read chunks for this page
  64. chunks = []
  65. for _ in range(6):
  66. data = f.read(512)
  67. if len(data) > 0:
  68. print 'store:', data.encode('hex')
  69. chunks.append(data)
  70. if len(data) < 512:
  71. break
  72.  
  73. # write qr to page
  74. if len(chunks) > 0:
  75. chunks = map(to_qr, chunks)
  76. to_page(c, chunks)
  77. c.showPage()
  78. if len(chunks) < 6:
  79. break
  80. c.save()
  81. print 'done'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement