-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: script to generate QR codes (#454)
- Loading branch information
Showing
11 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[flake8] | ||
max-line-length = 127 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.DS_Store | ||
qr_codes/*.png | ||
!qr_codes/calitp.org.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |