×
Dustin Lennon

Dustin Lennon

Applied Scientist
dlennon.org

 

Command Line QR Codes

A simple python script to generate a QR code from a URL


Dustin Lennon
December 2023
https://dlennon.org/20231211_urltoqr
December 2023


Abstract

Abstract

This post describes a python command line utility that momentarily displays a URL on the screen.

A Use Case

A Use Case

Once in a while, you have a long URL on a laptop or desktop and need to access it on a phone. For instance, there are some mobile apps that have adopted this pattern of initial configuration and set up.

If there are security concerns–say the link has an embedded token–emailing the link to yourself may not be ideal. Transferring the same information through the cloud might be seen to carry similar risk.

Another option would be to copy the link to the clipboard, pass it through a local qr code command line utility, and display it momentarily on the screen for capture by the phone’s qr reader. At the very least, with this approach, there’s no need to have the URL exist anywhere but on the local machine and the phone.

Usage

Usage

python3 script.py --url https://dlennon.org

and the script opens up a window which is closed by a key-press.

QR code: https://dlennon.org
Details

Details

This is a simple, straightforward script that

  • parses the command line argument;
  • creates the QR image;
  • creates a matplotlib figure and displays the image;
  • adds a key-press exit handler
Command Line Arguments

We use the argparse package and choose a self-serving URL as the default.

parser = argparse.ArgumentParser()
parser.add_argument('--url')

if len( sys.argv ) > 1:
  ns = parser.parse_args()
else:
  args = shlex.split("--url https://dlennon.org")
  ns = parser.parse_args( args )
Create the QR image
img = qrcode.make(ns.url)  # an instance of qrcode.image.pil.PilImage
Create the matplotlib figure
mpl.interactive(True)
plt.close('all')
fig = plt.figure('qr', figsize=(10,10))
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

This makes it quick to exit the program. UI focus can be either the console or the window containing the QR code, the default.

def on_press(event):
  sys.stdout.flush()
  sys.exit(0)

fig.canvas.callbacks.connect('key_press_event', on_press)
Appendix

Appendix

full script
import sys
import io
import argparse
import shlex
import qrcode

import matplotlib as mpl
import matplotlib.pyplot as plt

# Command Line Parsing
parser = argparse.ArgumentParser()
parser.add_argument('--url')
parser.add_argument('--size', default=10, type=int)

if len( sys.argv ) > 1:
  ns = parser.parse_args()
else:
  args = shlex.split("--url https://dlennon.org")
  ns = parser.parse_args( args )

# Create QR image
img = qrcode.make(ns.url)


# Create matplotlib figure and display the image
mpl.interactive(True)
plt.close('all')
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
def on_press(event):
  sys.exit(0)

fig.canvas.callbacks.connect('key_press_event', on_press)

input("press <enter> to exit")
requirements.txt

Generated by pipenv requirements:

-i https://pypi.org/simple
contourpy==1.2.0; python_version >= '3.9'
cycler==0.12.1; python_version >= '3.8'
fonttools==4.46.0; python_version >= '3.8'
kiwisolver==1.4.5; python_version >= '3.7'
matplotlib==3.8.2; python_version >= '3.9'
numpy==1.26.2; python_version >= '3.9'
packaging==23.2; python_version >= '3.7'
pillow==10.1.0; python_version >= '3.8'
pyparsing==3.1.1; python_full_version >= '3.6.8'
pypng==0.20220715.0
python-dateutil==2.8.2; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2'
qrcode[pil]==7.4.2; python_version >= '3.7'
six==1.16.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2'
typing-extensions==4.9.0; python_version >= '3.8'