Kaludi/QR-Code-Generator-Streamlit_App
7
1import streamlit as st2import qrcode3from qrcode.image.pil import PilImage4from PIL import Image5import io6import base647from urllib.parse import urlparse8 9 10# Function to convert image to base6411def get_image_as_base64(image: Image):12 buffer = io.BytesIO()13 image.save(buffer, format="PNG")14 image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')15 return image_base6416 17def get_url_filename(url):18 parsed_uri = urlparse(url)19 domain = '{uri.netloc}'.format(uri=parsed_uri)20 main_domain = domain.split('.')21 main_domain = main_domain[1] if main_domain[0] == 'www' else main_domain[0]22 path = parsed_uri.path.strip('/').replace('/', '_')23 return f"{main_domain}_{path}" if path else main_domain24 25 26 27 28# Streamlit app title29st.title("Bulk QR Code Generator")30st.write("This is a simple Streamlit web app for generating QR codes based on user input. You can choose between generating a QR code for a URL or plain text with the ability to generate multiple URLs at once.")31 32# QR code content options33qr_content_options = ["URL", "Text"]34# qr_content_options = ["URL", "Text", "Contact Information"]35qr_content_type = st.selectbox("Select QR content type", qr_content_options)36 37if qr_content_type == "Contact Information":38 first_name = st.text_input("First Name")39 last_name = st.text_input("Last Name")40 phone = st.text_input("Phone Number")41 email = st.text_input("Email Address")42 content = f"BEGIN:VCARD\nVERSION:3.0\nN:{last_name};{first_name}\nFN:{first_name} {last_name}\nTEL;TYPE=CELL:{phone}\nEMAIL:{email}\nEND:VCARD"43else:44 content = st.text_area("Enter your content (one per line for multiple QR codes)", height=150)45 46if st.button("Generate QR Code"):47 if content:48 contents = content.split("\n")49 50 for i, c in enumerate(contents):51 if c.strip():52 # Generate QR code53 qr = qrcode.QRCode(54 version=1,55 error_correction=qrcode.constants.ERROR_CORRECT_H,56 box_size=10,57 border=458 )59 qr.add_data(c)60 qr.make(fit=True)61 62 img = qr.make_image(fill_color="black", back_color="white", image_factory=PilImage)63 64 # Convert PilImage to bytes-like object65 buffer = io.BytesIO()66 img.save(buffer, format="PNG")67 img_bytes = buffer.getvalue()68 69 img_base64 = get_image_as_base64(img)70 71 st.markdown(f"##### {c}")72 st.image(img_bytes, caption=f"QR code for {c}", use_column_width=True)73 file_name = get_url_filename(c) if qr_content_type == "URL" else f"QR_{i}"74 st.markdown(f'<a href="data:image/png;base64,{img_base64}" download="{file_name}.png" style="display:inline-block;background-color:#4CAF50;border:none;color:white;padding:8px 16px;text-align:center;text-decoration:none;font-size:16px;margin:4px 2px;cursor:pointer;">Download QR code</a>', unsafe_allow_html=True)75 else:76 st.error("Please enter content for the QR code.")77 78 79 80 