Skip to content

Commit

Permalink
Feat: script to generate QR codes (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaveman authored Mar 27, 2024
2 parents d50750a + 67d7eec commit 8a8fe07
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 9 deletions.
18 changes: 17 additions & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
FROM python:3.11

WORKDIR /customer-success
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
USER=calitp

RUN useradd --create-home --shell /bin/bash $USER && \
chown -R $USER /home/$USER

USER $USER
ENV PATH "$PATH:/home/$USER/.local/bin"
WORKDIR /home/$USER/app

RUN python -m pip install --upgrade pip

WORKDIR /home/$USER/customer-success

COPY docs/requirements.txt docs/requirements.txt
RUN pip install -r docs/requirements.txt

COPY qr_codes/requirements.txt qr_codes/requirements.txt
RUN pip install -r qr_codes/requirements.txt
12 changes: 8 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
{
"name": "cal-itp/customer-success",
"dockerComposeFile": ["../compose.yml"],
"service": "docs",
"runServices": ["docs"],
"workspaceFolder": "/customer-success",
"service": "dev",
"runServices": ["dev"],
"workspaceFolder": "/home/calitp/customer-success",
"postStartCommand": [],
"postAttachCommand": [],
"customizations": {
Expand All @@ -20,8 +20,12 @@
"extensions": [
"DavidAnson.vscode-markdownlint",
"eamodio.gitlens",
"esbenp.prettier-vscode",
"mhutchie.git-graph",
"esbenp.prettier-vscode"
"ms-python.python",
"ms-python.black-formatter",
"ms-python.flake8",
"SimonSiefke.svg-preview"
]
}
}
Expand Down
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 127
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
qr_codes/*.png
!qr_codes/calitp.org.png
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
"request": "launch",
"module": "mkdocs",
"args": ["serve", "--dev-addr", "0.0.0.0:8000"]
},
{
"name": "QR Code",
"type": "python",
"request": "launch",
"cwd": "${workspaceRoot}/qr_codes",
"program": "main.py",
"args": [
"https://www.calitp.org",
"calitp.org.png",
"--color=#ffffff",
"--background=#046b99",
"--size=6"
]
}
]
}
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
"editor.unicodeHighlight.ambiguousCharacters": false,
"editor.unicodeHighlight.invisibleCharacters": false,
"editor.wordWrap": "on"
}
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.languageServer": "Pylance"
}
6 changes: 3 additions & 3 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: customer-success

services:
docs:
dev:
build:
context: .
dockerfile: .devcontainer/Dockerfile
image: cal-itp/customer-success:docs
image: cal-itp/customer-success:dev
entrypoint: []
command: sleep infinity
ports:
- "8000"
volumes:
- ./:/customer-success
- ./:/home/calitp/customer-success
25 changes: 25 additions & 0 deletions qr_codes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generate QR codes

> Generate a QR code as an PNG file
1. Open this repository's devcontainer in VS Code
2. Change into the `qr_codes` directory
3. Run the helper script

```bash
$ python main.py -h
usage: main.py [-h] [--color COLOR] [--background BACKGROUND] [--size SIZE] data output

Generate a QR code as an PNG file

positional arguments:
data The data to encode as a QR code
output Path to an PNG file where the QR code is written

options:
-h, --help show this help message and exit
--color COLOR The color of the QR code in hex format, by default black
--background BACKGROUND
The background color of the QR code in hex format, by default transparent
--size SIZE The QR code size from 1-10, by default 4
```
Binary file added qr_codes/calitp.org.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions qr_codes/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import argparse
import sys

from PIL import ImageColor
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.colormasks import SolidFillColorMask


def generate(args: argparse.Namespace):
qr = qrcode.QRCode(version=args.size, image_factory=StyledPilImage)
qr.add_data(args.data)
qr.make(fit=True)

# convert hex colors to RGB tuples, adding alpha channel to the front_color
# alpha = 0 means transparent
# alpha = 255 means opaque
front_color = ImageColor.getrgb(args.color) + (255,)
back_color = (
# convert the string "transparent" to RGBA with full transparency
(255, 255, 255, 0)
if args.background == "transparent"
# if we have a background color, it doesn't need transparency/alpha
else ImageColor.getrgb(args.background)
)
color_mask = SolidFillColorMask(back_color, front_color)

img = qr.make_image(color_mask=color_mask)
img.save(args.output)


def main(argv=None):
argv = argv if argv is not None else sys.argv[1:]
parser = argparse.ArgumentParser(
prog="main.py",
description="Generate a QR code as an PNG file",
)

parser.add_argument("data", help="The data to encode as a QR code")
parser.add_argument(
"output", help="Path to an PNG file where the QR code is written"
)
parser.add_argument(
"--color",
help="The color of the QR code in hex format, by default black",
default="#000000",
)
parser.add_argument(
"--background",
help="The background color of the QR code in hex format, by default transparent",
default="transparent",
)
parser.add_argument(
"--size",
help="The QR code size from 1-10, by default 4",
default=4,
)

args = parser.parse_args(argv)

generate(args)

return 0


if __name__ == "__main__":
raise SystemExit(main())
3 changes: 3 additions & 0 deletions qr_codes/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pypng==0.20220715.0
qrcode[pil]==7.4.2
typing_extensions==4.10.0

0 comments on commit 8a8fe07

Please sign in to comment.