Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # pip install qrcode[pil]
- import qrcode
- from qrcode.image.styledpil import StyledPilImage
- from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer
- from qrcode.image.styles.colormasks import RadialGradiantColorMask
- import argparse
- # Function to create a customized QR code
- def create_custom_qr_code(url, error_correction_level, logo_path, output_file):
- # Map error correction levels
- error_correction_map = {
- "L": qrcode.constants.ERROR_CORRECT_L, # ~7% of data can be restored
- "M": qrcode.constants.ERROR_CORRECT_M, # ~15% of data can be restored
- "Q": qrcode.constants.ERROR_CORRECT_Q, # ~25% of data can be restored
- "H": qrcode.constants.ERROR_CORRECT_H, # ~30% of data can be restored
- }
- # Create the QR code instance with the specified error correction level
- qr = qrcode.QRCode(error_correction=error_correction_map[error_correction_level])
- qr.add_data(url)
- # Generate the image with custom styles
- img = qr.make_image(
- image_factory=StyledPilImage,
- embeded_image_path=logo_path,
- module_drawer=RoundedModuleDrawer(),
- color_mask=RadialGradiantColorMask(),
- )
- # Save the QR code image
- img.save(output_file)
- print(f"QR code saved to {output_file}")
- if __name__ == "__main__":
- # Set up argument parsing for dynamic input
- parser = argparse.ArgumentParser(
- description="Generate a styled QR code with customizable parameters."
- )
- parser.add_argument(
- "--url",
- type=str,
- required=True,
- help="The URL or data to encode in the QR code.",
- )
- parser.add_argument(
- "--error_correction",
- type=str,
- choices=["L", "M", "Q", "H"],
- default="H",
- help="Error correction level: L (~7%), M (~15%), Q (~25%), H (~30%). Default is H.",
- )
- parser.add_argument(
- "--logo",
- type=str,
- required=True,
- help="Path to the logo image to embed in the QR code.",
- )
- parser.add_argument(
- "--output",
- type=str,
- default="styled_qr_code.png",
- help="Output file path for the QR code image.",
- )
- # Parse arguments
- args = parser.parse_args()
- # Create the QR code with provided parameters
- create_custom_qr_code(args.url, args.error_correction, args.logo, args.output)
Add Comment
Please, Sign In to add comment