Skip to content

Instantly share code, notes, and snippets.

@guangningyu
Created December 4, 2016 03:04
Show Gist options
  • Save guangningyu/c9744f68148ff92515a3864921f84dae to your computer and use it in GitHub Desktop.
Save guangningyu/c9744f68148ff92515a3864921f84dae to your computer and use it in GitHub Desktop.
Generate QRCode from text using Python.
import sys
import qrcode
LINES_PER_PARA = 60
def split_file(infile):
para_list = []
para = []
with open(infile, 'rb') as f:
for line in f.readlines():
para.append(line.strip() + '\r\n')
if len(para) == LINES_PER_PARA:
para_list.append(para)
para = []
if para:
para_list.append(para)
return para_list
def save_qrcode(para, outfile):
qr = qrcode.QRCode()
for line in para:
qr.add_data(line.strip() + '\r\n')
qr.make(fit=True)
img = qr.make_image()
img.save(outfile)
if __name__ == '__main__':
infile = sys.argv[1]
try:
outfile = sys.argv[2].split('\\')[-1].split('.')[0] + '_{0}' + '.png'
except Exception as e:
outfile = infile.split('\\')[-1].split('.')[0] + '_{0}' + '.png'
para_list = split_file(infile)
for para in para_list:
para_no = para_list.index(para)
save_qrcode(para, outfile.format('%02d' % para_no))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment