JDavis147

QR code generator

Oct 15th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | Software | 0 0
  1. # pip install qrcode[pil]
  2.  
  3. import qrcode
  4. from qrcode.image.styledpil import StyledPilImage
  5. from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer
  6. from qrcode.image.styles.colormasks import RadialGradiantColorMask
  7. import argparse
  8.  
  9.  
  10. # Function to create a customized QR code
  11. def create_custom_qr_code(url, error_correction_level, logo_path, output_file):
  12.     # Map error correction levels
  13.     error_correction_map = {
  14.         "L": qrcode.constants.ERROR_CORRECT_L,  # ~7% of data can be restored
  15.         "M": qrcode.constants.ERROR_CORRECT_M,  # ~15% of data can be restored
  16.         "Q": qrcode.constants.ERROR_CORRECT_Q,  # ~25% of data can be restored
  17.         "H": qrcode.constants.ERROR_CORRECT_H,  # ~30% of data can be restored
  18.     }
  19.  
  20.     # Create the QR code instance with the specified error correction level
  21.     qr = qrcode.QRCode(error_correction=error_correction_map[error_correction_level])
  22.     qr.add_data(url)
  23.  
  24.     # Generate the image with custom styles
  25.     img = qr.make_image(
  26.         image_factory=StyledPilImage,
  27.         embeded_image_path=logo_path,
  28.         module_drawer=RoundedModuleDrawer(),
  29.         color_mask=RadialGradiantColorMask(),
  30.     )
  31.  
  32.     # Save the QR code image
  33.     img.save(output_file)
  34.     print(f"QR code saved to {output_file}")
  35.  
  36.  
  37. if __name__ == "__main__":
  38.     # Set up argument parsing for dynamic input
  39.     parser = argparse.ArgumentParser(
  40.         description="Generate a styled QR code with customizable parameters."
  41.     )
  42.     parser.add_argument(
  43.         "--url",
  44.         type=str,
  45.         required=True,
  46.         help="The URL or data to encode in the QR code.",
  47.     )
  48.     parser.add_argument(
  49.         "--error_correction",
  50.         type=str,
  51.         choices=["L", "M", "Q", "H"],
  52.         default="H",
  53.         help="Error correction level: L (~7%), M (~15%), Q (~25%), H (~30%). Default is H.",
  54.     )
  55.     parser.add_argument(
  56.         "--logo",
  57.         type=str,
  58.         required=True,
  59.         help="Path to the logo image to embed in the QR code.",
  60.     )
  61.     parser.add_argument(
  62.         "--output",
  63.         type=str,
  64.         default="styled_qr_code.png",
  65.         help="Output file path for the QR code image.",
  66.     )
  67.  
  68.     # Parse arguments
  69.     args = parser.parse_args()
  70.  
  71.     # Create the QR code with provided parameters
  72.     create_custom_qr_code(args.url, args.error_correction, args.logo, args.output)
  73.  
Add Comment
Please, Sign In to add comment