Spaces:
Sleeping
Sleeping
import streamlit as st | |
import qrcode | |
from PIL import Image | |
from io import BytesIO | |
# Function to generate QR code | |
def generate_qr_code(data): | |
qr = qrcode.QRCode( | |
version=1, | |
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") | |
return img | |
# Streamlit app | |
st.title('QR Code Generator') | |
st.write('Enter text or a URL to generate a QR code.') | |
# User input | |
user_input = st.text_input("Enter text or URL:") | |
if user_input: | |
# Generate QR code | |
img = generate_qr_code(user_input) | |
# Convert the PIL image to a bytes-like object | |
buffer = BytesIO() | |
img.save(buffer, format="PNG") | |
buffer.seek(0) | |
img_bytes = buffer.getvalue() | |
# Display QR code | |
st.image(img_bytes, caption='Generated QR Code') | |
# Option to download the QR code | |
st.download_button( | |
label="Download QR Code", | |
data=buffer, | |
file_name="qr_code.png", | |
mime="image/png" | |
) | |