Gradio-QR-Code / app.py
XMMR12's picture
Upload 2 files
a7dd399 verified
import gradio as gr
import qrcode
from PIL import Image
import io
def generate_qrcode(text):
"""
Generate a QR code image from input text
"""
# Create QR code instance
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
# Add data to the QR code
qr.add_data(text)
qr.make(fit=True)
# Create an image from the QR code
img = qr.make_image(fill_color="black", back_color="white")
# Convert to PIL Image
pil_img = img.get_image()
return pil_img
# Create Gradio interface
with gr.Blocks() as app:
gr.Markdown("## 🧾 QR Code Generator")
gr.Markdown("Enter text or a URL to generate a QR code")
with gr.Row():
text_input = gr.Textbox(
label="Input Text/URL",
placeholder="Enter text or URL here...",
max_lines=1
)
with gr.Row():
generate_btn = gr.Button("Generate QR Code")
with gr.Row():
image_output = gr.Image(label="Generated QR Code")
# Set up event handler
generate_btn.click(
fn=generate_qrcode,
inputs=text_input,
outputs=image_output
)
# Example inputs
gr.Examples(
examples=[
"https://www.example.com",
"Hello World!",
"mailto:[email protected]",
"WIFI:S:MyNetwork;T:WPA;P:MyPassword;;"
],
inputs=text_input
)
# Launch the app
app.launch()