CultriX's picture
Update app.py
550ae51 verified
raw
history blame
3.47 kB
import qrcode
from PIL import Image
import gradio as gr
import tempfile
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
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
img.save(temp_file.name, format="PNG")
temp_file.close() # Ensure file is written to disk
return temp_file.name # Return the file path only, Gradio will handle displaying the image
# Function to read a QR code
def read_qr(image_file):
# Convert PIL image to a NumPy array
img = Image.open(image_file.name)
img_np = np.array(img)
# Convert RGB to BGR as OpenCV expects
if img_np.ndim == 3:
img_np = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
# Initialize OpenCV QR code detector
detector = cv2.QRCodeDetector()
data, _, _ = detector.detectAndDecode(img_np)
return data if data else "No QR code found."
# Function to copy text to clipboard (this requires platform-specific implementations)
def copy_to_clipboard(text):
import pyperclip
pyperclip.copy(text)
return "Copied to Clipboard!"
# Gradio Interface
def create_gradio_interface():
# QR Code Generator with Display and Downloadable Link
def generate_qr_interface(data):
qr_file = generate_qr(data) # This will now return the file path directly
qr_image = Image.open(qr_file)
return [qr_image, qr_file] # Return both image and file for Gradio to display/download
# QR Code Reader with Copy-to-Clipboard Button
def read_qr_interface(image_file):
decoded_data = read_qr(image_file)
return decoded_data # Return decoded text
# QR Code Generator Tab
generate_interface = gr.Interface(
fn=generate_qr_interface,
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
outputs=[
gr.Image(label="Generated QR Code"), # Display the QR code image
gr.File(label="Download QR Code"), # Downloadable link for the QR code file
],
title="Generate QR Code",
description="Quickly create a QR code from any text or URL.",
)
# QR Code Reader Tab
read_interface = gr.Interface(
fn=read_qr_interface,
inputs=gr.File(type="file", label="Upload QR Code Image"),
outputs=gr.Textbox(label="Decoded Data"),
title="Read QR Code",
description="Upload an image with a QR code to decode the embedded data.",
)
# Clipboard Functionality for "Read QR Code" Tab
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"):
qr_text = gr.Textbox(label="Decoded Data") # Single box for decoded data
copy_button = gr.Button("Copy to Clipboard")
read_interface.render()
# Connect button click to copy function
copy_button.click(
fn=copy_to_clipboard,
inputs=qr_text,
outputs=qr_text,
)
demo.launch(share=True)
# Run the Gradio interface
create_gradio_interface()