Created
April 11, 2024 07:31
-
-
Save lkluft/629b1ec49292c3e429a1fc44ae076004 to your computer and use it in GitHub Desktop.
Script to generate a simple QR code using the qrcode python package
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
import argparse | |
import qrcode | |
from qrcode.image.styledpil import StyledPilImage | |
from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer | |
from qrcode.image.styles.colormasks import ( | |
HorizontalGradiantColorMask, | |
SolidFillColorMask, | |
) | |
parser = argparse.ArgumentParser( | |
description="The program generates a QR code for a given URL and optionally adds an image to the QRcode center" | |
) | |
parser.add_argument("url", help="type the url you want to link to here") | |
parser.add_argument( | |
"--image", | |
default=None, | |
help="you can specify an image that shall be inseted in the center of the QR code.", | |
) | |
parser.add_argument( | |
"-o", | |
"--ofile", | |
default="qrcode.png", | |
help="you can specify an output file here, including the fileformat though the respective ending, e.g. .png", | |
) | |
args = parser.parse_args() | |
def make_qr_code(url, image=None): | |
qr = qrcode.QRCode(box_size=20, error_correction=qrcode.constants.ERROR_CORRECT_Q) | |
qr.add_data(url) | |
qr_img = qr.make_image( | |
image_factory=StyledPilImage, | |
module_drawer=RoundedModuleDrawer(0.618), | |
# color_mask=SolidFillColorMask(front_color=(0, 110, 112)), # MPG | |
# color_mask=SolidFillColorMask(front_color=(209, 26, 26)), # Staatsoper rot | |
color_mask=SolidFillColorMask(back_color=(204, 196, 154)), # Staatsoper braun | |
embeded_image_path=image, | |
) | |
return qr_img | |
def __main__(): | |
ret = make_qr_code(url=args.url, image=args.image) | |
ret.save(args.ofile) | |
if __name__ == "__main__": | |
__main__() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment