Skip to content

Instantly share code, notes, and snippets.

@dustinlennon
Last active October 5, 2024 22:04
Show Gist options
  • Save dustinlennon/663f4964cdc36bc5cf21f7f1cd2845c4 to your computer and use it in GitHub Desktop.
Save dustinlennon/663f4964cdc36bc5cf21f7f1cd2845c4 to your computer and use it in GitHub Desktop.
qr code from url
#!/usr/bin/env python3
"""Generate a QR code from a URL
usage: qr.py [-h] [--url URL] [--interactive] [--output OUTPUT] [--size SIZE]
options:
-h, --help show this help message and exit
--url URL
--interactive
--output OUTPUT a PNG file
--size SIZE
"""
import sys
import argparse
import qrcode
import matplotlib as mpl
import matplotlib.pyplot as plt
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--url')
parser.add_argument("--interactive", action = "store_true")
parser.add_argument("--output", default = None, help = "a PNG file")
parser.add_argument('--size', default=10, type=int)
ns = parser.parse_args()
# Create the QR image
img = qrcode.make(ns.url)
if ns.interactive:
# Create a matplotlib figure and display the image
mpl.interactive(True)
fig = plt.figure('qr', figsize=(ns.size,ns.size))
ax = fig.gca()
ax.imshow(img, cmap='gray', vmin=0, vmax = 1)
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])
# Add a key-press exit handler to the figure
fig.canvas.callbacks.connect('key_press_event', lambda event: sys.exit(0))
input("press <enter> to exit")
else:
if ns.output is None:
parser.error("either --interactive or --output must be specified")
img.save(ns.output, "PNG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment