Skip to content

Instantly share code, notes, and snippets.

@catichenor
Created April 16, 2019 19:43
Show Gist options
  • Save catichenor/a920922d3beb9bae9fcf9db2783cc814 to your computer and use it in GitHub Desktop.
Save catichenor/a920922d3beb9bae9fcf9db2783cc814 to your computer and use it in GitHub Desktop.
Generate a QR code from text in an input file (also adds the text itself).
#!/usr/bin/env python
import os
import argparse
from pathlib import Path
import qrcode
from PIL import Image, ImageDraw, ImageFont
argparser = argparse.ArgumentParser()
argparser.add_argument("input", help="path to file containing text")
args = argparser.parse_args()
with open(args.input, 'r') as infile:
input_text = infile.read()
img = qrcode.make(input_text)
qr_pil = Image.new('RGB', (img.pixel_size, img.pixel_size), 'white')
qr_pil.paste(img, (0, 0))
qr_text = ImageDraw.Draw(qr_pil)
qr_text.text((0, 0), input_text, (0, 0, 0))
qr_savepath = str(Path.home() / "Pictures" / "qr.png")
qr_pil.save(qr_savepath)
os.startfile(qr_savepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment