Content is user-generated and unverified.
# QR Code Generator with Python # First, install the required library: # pip install qrcode[pil] import qrcode from PIL import Image def generate_basic_qr(data, filename="qr_code.png"): """Generate a basic QR code""" qr = qrcode.QRCode( version=1, # Controls the size of the QR code error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) qr.add_data(data) qr.make(fit=True) img = qr.make_image(fill_color="black", back_color="white") img.save(filename) print(f"QR code saved as {filename}") return img def generate_custom_qr(data, filename="custom_qr.png"): """Generate a customized QR code with colors and styling""" qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_M, box_size=10, border=4, ) qr.add_data(data) qr.make(fit=True) # Create QR code with custom colors img = qr.make_image(fill_color="darkblue", back_color="lightblue") img.save(filename) print(f"Custom QR code saved as {filename}") return img def generate_qr_with_logo(data, logo_path, filename="qr_with_logo.png"): """Generate QR code with a logo in the center""" qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_H, # High error correction for logo box_size=10, border=4, ) qr.add_data(data) qr.make(fit=True) img = qr.make_image(fill_color="black", back_color="white") # Add logo if provided if logo_path: try: logo = Image.open(logo_path) # Calculate logo size (should be about 1/5 of QR code size) qr_width, qr_height = img.size logo_size = min(qr_width, qr_height) // 5 # Resize logo logo = logo.resize((logo_size, logo_size), Image.Resampling.LANCZOS) # Calculate position to center the logo logo_pos = ((qr_width - logo_size) // 2, (qr_height - logo_size) // 2) # Paste logo onto QR code img.paste(logo, logo_pos) except Exception as e: print(f"Error adding logo: {e}") img.save(filename) print(f"QR code with logo saved as {filename}") return img # Example usage if __name__ == "__main__": # Example 1: Basic QR code text_data = "Hello, World! This is a QR code generated with Python." generate_basic_qr(text_data, "basic_qr.png") # Example 2: URL QR code url_data = "https://www.python.org" generate_basic_qr(url_data, "url_qr.png") # Example 3: Custom colored QR code generate_custom_qr(text_data, "custom_qr.png") # Example 4: QR code with logo (uncomment if you have a logo file) # generate_qr_with_logo(url_data, "logo.png", "qr_with_logo.png") # Example 5: WiFi QR code wifi_data = "WIFI:T:WPA;S:YourNetworkName;P:YourPassword;;" generate_basic_qr(wifi_data, "wifi_qr.png") # Example 6: Contact information QR code (vCard format) contact_data = """BEGIN:VCARD VERSION:3.0 FN:John Doe ORG:Example Company TEL:+1234567890 EMAIL:john.doe@example.com URL:https://johndoe.com END:VCARD""" generate_basic_qr(contact_data, "contact_qr.png") print("All QR codes generated successfully!") # Advanced QR code with different error correction levels def generate_qr_different_sizes(): """Generate QR codes with different sizes and error correction levels""" data = "Python QR Code Generation Example" # Different error correction levels error_levels = { 'L': qrcode.constants.ERROR_CORRECT_L, # ~7% error correction 'M': qrcode.constants.ERROR_CORRECT_M, # ~15% error correction 'Q': qrcode.constants.ERROR_CORRECT_Q, # ~25% error correction 'H': qrcode.constants.ERROR_CORRECT_H, # ~30% error correction } for level_name, level_const in error_levels.items(): qr = qrcode.QRCode( version=1, error_correction=level_const, box_size=8, border=4, ) qr.add_data(data) qr.make(fit=True) img = qr.make_image(fill_color="black", back_color="white") filename = f"qr_error_level_{level_name}.png" img.save(filename) print(f"QR code with error correction level {level_name} saved as {filename}") # Uncomment to generate QR codes with different error correction levels # generate_qr_different_sizes()
Content is user-generated and unverified.
    QR Code Generator with Python | Claude