Spaces:
Running
Running
import qrcode | |
import cv2 | |
from PIL import Image | |
import gradio as gr | |
import tempfile | |
import os | |
import numpy as np | |
# Function to generate a QR code | |
def generate_qr(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="black", back_color="white") | |
# Save to a temporary file and return the file path | |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png") | |
img.save(temp_file.name, format="PNG") | |
temp_file.close() # Close the file to flush contents to disk | |
return temp_file.name # Return the file path | |
# Function to read a QR code using OpenCV | |
def read_qr(img): | |
# Convert PIL image to a NumPy array | |
img = np.array(img) | |
# Convert RGB to BGR format as OpenCV expects BGR | |
if img.ndim == 3: | |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) | |
# Initialize OpenCV QR code detector | |
detector = cv2.QRCodeDetector() | |
data, _, _ = detector.detectAndDecode(img) | |
return data if data else "No QR code found." | |
# Gradio interface with functional buttons | |
def create_gradio_interface(): | |
# QR Code Generator with Downloadable File | |
def generate_qr_with_download(data): | |
qr_file = generate_qr(data) # Generate QR code file | |
return qr_file # Return file directly as downloadable output | |
# QR Code Reader with Copy-to-Clipboard functionality | |
def read_qr_with_copy(img): | |
decoded_data = read_qr(img) # Decode QR code | |
return decoded_data, decoded_data # Return decoded text for display and copy | |
# Generator Interface | |
generate_interface = gr.Interface( | |
fn=generate_qr_with_download, | |
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"), | |
outputs=gr.File(label="Download Generated QR Code"), # File download | |
title="Generate QR Code", | |
description="Quickly create a QR code from any text or URL.", | |
) | |
# Reader Interface | |
read_interface = gr.Interface( | |
fn=read_qr_with_copy, | |
inputs=gr.Image(type="pil", label="Upload QR Code Image"), | |
outputs=[ | |
gr.Textbox(label="Decoded Data"), # Decoded QR code text | |
gr.Textbox(visible=False), # Invisible output for clipboard content | |
], | |
title="Read QR Code", | |
description="Upload an image with a QR code to decode the embedded data.", | |
live=False, | |
) | |
# Add clipboard copy functionality using Gradio button callbacks | |
def on_copy(decoded_text): | |
return gr.Textbox.update(value=decoded_text) # Update clipboard content | |
# Custom UI with clipboard copy button | |
with gr.Blocks() as demo: | |
gr.Markdown("## QR Code Tool: Generate and Decode with Ease") | |
with gr.Tab("Generate QR Code"): | |
generate_interface.render() | |
with gr.Tab("Read QR Code"): | |
with gr.Row(): | |
qr_text = gr.Textbox(label="Decoded Data") | |
qr_clipboard = gr.Textbox(visible=False) # Clipboard value | |
copy_button = gr.Button("Copy to Clipboard") | |
read_interface.render() | |
copy_button.click(on_copy, qr_text, qr_clipboard) | |
demo.launch(share=True) | |
# Run the Gradio interface | |
create_gradio_interface() | |