Skip to content

Instantly share code, notes, and snippets.

@reneluria
Last active July 25, 2023 12:37
Show Gist options
  • Save reneluria/9a675d4a2a6e3a01cbaf019c58be7b3c to your computer and use it in GitHub Desktop.
Save reneluria/9a675d4a2a6e3a01cbaf019c58be7b3c to your computer and use it in GitHub Desktop.
Generate qrcode with optional logo
#!/usr/bin/env python3
import qrcode
import typer
from typing_extensions import Annotated, Optional
from rich import print
app = typer.Typer()
default_data = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
@app.command()
def gen_qr(
data: Annotated[
Optional[str],
typer.Argument(
help="data to encode, example: 'WIFI:S:mon-reseau;T:WPA2;P:mon-code;;'"
),
] = None,
output_filename: Annotated[
str, typer.Option("--output", help="output filename")
] = "qr.png",
background_image: Annotated[
str, typer.Option("--background", help="background image as logo")
] = None,
):
"""
Generate a qrcode using given data
Example data: 'WIFI:S:infowifi;T:WPA2;P:lewifiouvretes7chakras;;'
"""
QRcode = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
if data is None:
QRcode.add_data(default_data)
else:
QRcode.add_data(data)
QRcode.make()
# taking color name from user
QRcolor = "black"
# adding color to QR code
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer
from qrcode.image.styles.colormasks import RadialGradiantColorMask
QRimg = QRcode.make_image(
fill_color=QRcolor,
back_color="white",
image_factory=StyledPilImage,
module_drawer=RoundedModuleDrawer(),
color_mask=RadialGradiantColorMask(
edge_color=(0, 0, 0), center_color=(65, 137, 249)
),
embeded_image_path=background_image,
)
# save the QR code generated
QRimg.save(output_filename)
if data is None:
print(
"[bold red]It[/bold red] [bold pink]is[/bold pink] [bold blue]what it[/bold blue] [bold pink]is[/bold pink]"
)
print(f"qrcode generated, saved to {output_filename}")
if __name__ == "__main__":
app()
@reneluria
Copy link
Author

Setup

pip install "typer[all]" "qrcode[pil]"

@reneluria
Copy link
Author

Example:

qr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment