Last active
July 25, 2023 12:37
-
-
Save reneluria/9a675d4a2a6e3a01cbaf019c58be7b3c to your computer and use it in GitHub Desktop.
Generate qrcode with optional logo
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
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup