🎨 Beautiful QR Code Generator
Create stunning, customizable QR codes with advanced styling options
import streamlit as st import qrcode from PIL import Image, ImageDraw, ImageFont import io import base64 # Page configuration st.set_page_config( page_title="QR Code Generator", page_icon="📱", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS for beautiful styling st.markdown(""" """, unsafe_allow_html=True) def hex_to_rgb(hex_color): """Convert hex color to RGB tuple""" hex_color = hex_color.lstrip('#') return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) def create_gradient_image(size, start_color, end_color, direction='horizontal'): """Create a gradient image""" image = Image.new('RGB', size) draw = ImageDraw.Draw(image) if direction == 'horizontal': for x in range(size[0]): ratio = x / size[0] r = int(start_color[0] * (1 - ratio) + end_color[0] * ratio) g = int(start_color[1] * (1 - ratio) + end_color[1] * ratio) b = int(start_color[2] * (1 - ratio) + end_color[2] * ratio) draw.line([(x, 0), (x, size[1])], fill=(r, g, b)) else: # vertical for y in range(size[1]): ratio = y / size[1] r = int(start_color[0] * (1 - ratio) + end_color[0] * ratio) g = int(start_color[1] * (1 - ratio) + end_color[1] * ratio) b = int(start_color[2] * (1 - ratio) + end_color[2] * ratio) draw.line([(0, y), (size[0], y)], fill=(r, g, b)) return image def create_rounded_qr(qr_img, corner_radius=20): """Create rounded corners for QR code""" # Create mask for rounded corners size = qr_img.size mask = Image.new('L', size, 0) draw = ImageDraw.Draw(mask) draw.rounded_rectangle([(0, 0), size], corner_radius, fill=255) # Apply mask qr_rounded = Image.new('RGBA', size, (255, 255, 255, 0)) qr_rounded.paste(qr_img, (0, 0)) qr_rounded.putalpha(mask) return qr_rounded def create_qr_code(data, fill_color, back_color, border, box_size, error_correction, style_type, gradient_end_color=None): """Generate QR code with custom styling""" # Error correction levels error_levels = { 'L (~7%)': qrcode.constants.ERROR_CORRECT_L, 'M (~15%)': qrcode.constants.ERROR_CORRECT_M, 'Q (~25%)': qrcode.constants.ERROR_CORRECT_Q, 'H (~30%)': qrcode.constants.ERROR_CORRECT_H } # Create QR code instance qr = qrcode.QRCode( version=1, error_correction=error_levels[error_correction], box_size=box_size, border=border, ) qr.add_data(data) qr.make(fit=True) # Generate base QR code if style_type == "Gradient" and gradient_end_color: # Create gradient background img = qr.make_image(fill_color='black', back_color='white') # Create gradient start_rgb = hex_to_rgb(fill_color) end_rgb = hex_to_rgb(gradient_end_color) gradient = create_gradient_image(img.size, start_rgb, end_rgb, 'horizontal') # Apply gradient to QR code img_array = img.load() gradient_array = gradient.load() for x in range(img.size[0]): for y in range(img.size[1]): if img_array[x, y] == 0: # Black pixel (QR code data) img_array[x, y] = gradient_array[x, y] else: # White pixel (background) img_array[x, y] = hex_to_rgb(back_color) else: img = qr.make_image(fill_color=fill_color, back_color=back_color) # Apply style modifications if style_type == "Rounded": img = create_rounded_qr(img, corner_radius=box_size*2) elif style_type == "With Logo Space": # Create space in center for logo draw = ImageDraw.Draw(img) center_x, center_y = img.size[0] // 2, img.size[1] // 2 logo_size = min(img.size) // 6 draw.rectangle([ center_x - logo_size//2, center_y - logo_size//2, center_x + logo_size//2, center_y + logo_size//2 ], fill=back_color, outline=fill_color, width=2) return img def get_qr_data_by_type(content_type): """Get QR data based on content type""" if content_type == "Text": return st.text_area("Enter your text:", height=100, placeholder="Hello World!") elif content_type == "URL": url = st.text_input("Enter URL:", placeholder="https://example.com") if url and not url.startswith(('http://', 'https://')): url = 'https://' + url return url elif content_type == "Email": email = st.text_input("Email address:", placeholder="contact@example.com") subject = st.text_input("Subject (optional):") body = st.text_area("Message (optional):", height=80) if email: params = [] if subject: params.append(f"subject={subject}") if body: params.append(f"body={body}") return f"mailto:{email}{'?' + '&'.join(params) if params else ''}" return "" elif content_type == "Phone": phone = st.text_input("Phone number:", placeholder="+1234567890") return f"tel:{phone}" if phone else "" elif content_type == "SMS": phone = st.text_input("Phone number:", placeholder="+1234567890") message = st.text_area("Message:", height=80, placeholder="Hello!") if phone: return f"sms:{phone}{'?body=' + message if message else ''}" return "" elif content_type == "WiFi": ssid = st.text_input("Network Name (SSID):", placeholder="MyWiFi") password = st.text_input("Password:", type="password") security = st.selectbox("Security Type", ["WPA", "WEP", "nopass"]) hidden = st.checkbox("Hidden Network") if ssid: return f"WIFI:T:{security};S:{ssid};P:{password};H:{'true' if hidden else 'false'};;" return "" elif content_type == "vCard": name = st.text_input("Full Name:", placeholder="John Doe") phone = st.text_input("Phone:", placeholder="+1234567890") email = st.text_input("Email:", placeholder="john@example.com") organization = st.text_input("Organization (optional):", placeholder="Company Inc.") if name: vcard = f"BEGIN:VCARD\nVERSION:3.0\nFN:{name}\n" if phone: vcard += f"TEL:{phone}\n" if email: vcard += f"EMAIL:{email}\n" if organization: vcard += f"ORG:{organization}\n" vcard += "END:VCARD" return vcard return "" # Main header st.markdown("""
Create stunning, customizable QR codes with advanced styling options
Your QR code is ready. Click below to download it as PNG.
🚀 Built with Streamlit | 💝 Made for QR code enthusiasts
💡 Tip: Bookmark this page for quick QR code generation!