-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathqr_code_generator.py
More file actions
67 lines (53 loc) · 1.62 KB
/
Copy pathqr_code_generator.py
File metadata and controls
67 lines (53 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
"""
QR Code Generator
------------------
Turns text or a URL into a QR code PNG image.
Install the required dependency first:
pip install "qrcode[pil]"
Usage:
python scripts/qr_code_generator.py "https://example.com" -o qr.png
"""
import argparse
import os
import sys
def generate_qr_code(data: str, output_path: str) -> None:
"""Generate a QR code PNG from `data` and save it to `output_path`."""
try:
import qrcode
except ImportError:
print('Missing dependency. Install it with:\n pip install "qrcode[pil]"')
sys.exit(1)
if os.path.exists(output_path):
print(
f"Error: '{output_path}' already exists. Choose a different "
f"--output path or remove the existing file first."
)
sys.exit(1)
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save(output_path)
width, height = img.size
print(f"Saved QR code to '{output_path}' ({width}x{height} pixels)")
def main():
parser = argparse.ArgumentParser(
description="Turn text or a URL into a QR code PNG."
)
parser.add_argument("data", help="The text or URL to encode")
parser.add_argument(
"-o",
"--output",
default="qr.png",
help="Path to save the PNG file (default: qr.png)",
)
args = parser.parse_args()
generate_qr_code(args.data, args.output)
if __name__ == "__main__":
main()