Spaces:
Running
Running
import os | |
import tempfile | |
import cv2 | |
import numpy as np | |
from PIL import Image | |
import gradio as gr | |
import pyperclip | |
def generate_qr_code(data): | |
""" | |
Generate a QR code from the given data. | |
Args: | |
data (str): The data to encode in the QR code. | |
Returns: | |
str: The path to the generated QR code image. | |
""" | |
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 | |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file: | |
img.save(temp_file.name, format="PNG") | |
return temp_file.name | |
def read_qr_code(img): | |
""" | |
Read a QR code from the given image. | |
Args: | |
img (PIL.Image): The image containing the QR code. | |
Returns: | |
str: The decoded data from the QR code. | |
""" | |
# Convert PIL image to a NumPy array | |
img_array = np.array(img) | |
# Convert RGB to BGR format as OpenCV expects BGR | |
if img_array.ndim == 3: | |
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) | |
# Initialize OpenCV QR code detector | |
detector = cv2.QRCodeDetector() | |
data, _, _ = detector.detectAndDecode(img_array) | |
return data if data else "No QR code found." | |
def download_qr_code(qr_image): | |
""" | |
Download the generated QR code image. | |
Args: | |
qr_image (str): The path to the generated QR code image. | |
""" | |
# Implement download logic here | |
pass | |
def copy_to_clipboard(decoded_text): | |
""" | |
Copy the decoded text to the clipboard. | |
Args: | |
decoded_text (str): The decoded text to copy. | |
""" | |
pyperclip.copy(decoded_text) | |
def create_gradio_interface(): | |
""" | |
Create the Gradio interface for generating and reading QR codes. | |
""" | |
# QR Code Generator Interface | |
generate_interface = gr.Interface( | |
fn=generate_qr_code, | |
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"), | |
outputs=gr.Image(label="Generated QR Code"), | |
title="Generate QR Code", | |
description="Quickly create a QR code from any text or URL.", | |
) | |
# Add download button to generated QR code | |
download_button = gr.Button("Download QR Code") | |
download_button.click(fn=lambda x: download_qr_code(x), inputs="Generated QR Code", outputs=None) | |
# QR Code Reader Interface | |
read_interface = gr.Interface( | |
fn=read_qr_code, | |
inputs=gr.Image(type="pil", 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.", | |
) | |
# Add copy to clipboard button to decoded text | |
copy_button = gr.Button("Copy to Clipboard") | |
copy_button.click(fn=lambda x: copy_to_clipboard(x), inputs="Decoded Data", outputs=None) | |
# Combine interfaces into a single tabbed layout | |
interface = gr.TabbedInterface( | |
[generate_interface, read_interface], | |
["Generate QR Code", "Read QR Code"] | |
) | |
# Launch interface with custom HTML for CSS styling | |
with gr.Blocks() as demo: | |
gr.HTML(custom_css) # Embed the custom CSS | |
interface.render() | |
demo.launch(share=True) | |
custom_css = """ | |
<style> | |
body {background-color: #1e1e2f; font-family: Arial, sans-serif; color: #e0e0e0;} | |
.gradio-container {max-width: 600px; margin: auto; padding: 20px; background-color: #2c2c3e; border-radius: 10px; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);} | |
h1, h2 {text-align: center; color: #f0f0f0; font-weight: 600;} | |
.gr-button {background-color: #3a3a4f; color: #e0e0e0; padding: 10px 20px; border-radius: 5px; font-size: 15px; border: none;} | |
.gr-button:hover {background-color: #505068;} | |
input, textarea,.gr-box {background-color: #3a3a4f; border: 1px solid #555; border-radius: 5px; padding: 10px; font-size: 14px; color: #e0e0e0;} | |
.gr-box:hover, input:hover, textarea:hover {border-color: #777;} | |
</style> | |
""" | |
create_gradio_interface() | |