File size: 3,122 Bytes
30a16e3 4b19f12 fa12f3d 30a16e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import streamlit as st
import qrcode
from qrcode.image.pil import PilImage
from PIL import Image
import io
import base64
from urllib.parse import urlparse
# Function to convert image to base64
def get_image_as_base64(image: Image):
buffer = io.BytesIO()
image.save(buffer, format="PNG")
image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
return image_base64
def get_url_filename(url):
parsed_uri = urlparse(url)
domain = '{uri.netloc}'.format(uri=parsed_uri)
main_domain = domain.split('.')
main_domain = main_domain[1] if main_domain[0] == 'www' else main_domain[0]
path = parsed_uri.path.strip('/').replace('/', '_')
return f"{main_domain}_{path}" if path else main_domain
# Streamlit app title
st.title("Bulk QR Code Generator")
st.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.")
# QR code content options
qr_content_options = ["URL", "Text"]
# qr_content_options = ["URL", "Text", "Contact Information"]
qr_content_type = st.selectbox("Select QR content type", qr_content_options)
if qr_content_type == "Contact Information":
first_name = st.text_input("First Name")
last_name = st.text_input("Last Name")
phone = st.text_input("Phone Number")
email = st.text_input("Email Address")
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"
else:
content = st.text_area("Enter your content (one per line for multiple QR codes)", height=150)
if st.button("Generate QR Code"):
if content:
contents = content.split("\n")
for i, c in enumerate(contents):
if c.strip():
# Generate QR code
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4
)
qr.add_data(c)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white", image_factory=PilImage)
# Convert PilImage to bytes-like object
buffer = io.BytesIO()
img.save(buffer, format="PNG")
img_bytes = buffer.getvalue()
img_base64 = get_image_as_base64(img)
st.markdown(f"##### {c}")
st.image(img_bytes, caption=f"QR code for {c}", use_column_width=True)
file_name = get_url_filename(c) if qr_content_type == "URL" else f"QR_{i}"
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)
else:
st.error("Please enter content for the QR code.")
|