Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,12 @@ import qrcode
|
|
| 2 |
import cv2
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
-
import
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
-
import os
|
| 8 |
|
| 9 |
|
| 10 |
-
# Function to generate a QR code
|
| 11 |
def generate_qr(data):
|
| 12 |
qr = qrcode.QRCode(
|
| 13 |
version=1,
|
|
@@ -19,11 +19,11 @@ def generate_qr(data):
|
|
| 19 |
qr.make(fit=True)
|
| 20 |
img = qr.make_image(fill="black", back_color="white")
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
img.save(
|
| 25 |
-
|
| 26 |
-
return
|
| 27 |
|
| 28 |
|
| 29 |
# Function to read a QR code
|
|
@@ -54,19 +54,18 @@ def create_gradio_interface():
|
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
qr_image = gr.Image(label="Generated QR Code")
|
| 57 |
-
|
| 58 |
|
| 59 |
def generate_qr_interface(data):
|
| 60 |
if not data.strip():
|
| 61 |
raise ValueError("Input text cannot be empty!")
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
return qr_image_pil, qr_file_path
|
| 65 |
|
| 66 |
generate_button.click(
|
| 67 |
generate_qr_interface,
|
| 68 |
inputs=data_input,
|
| 69 |
-
outputs=[qr_image,
|
| 70 |
)
|
| 71 |
|
| 72 |
# Tab for reading QR codes
|
|
@@ -75,7 +74,7 @@ def create_gradio_interface():
|
|
| 75 |
image_input = gr.Image(type="pil", label="Upload QR Code Image")
|
| 76 |
decode_button = gr.Button("Decode QR Code")
|
| 77 |
|
| 78 |
-
decoded_data = gr.Textbox(label="Decoded Data")
|
| 79 |
|
| 80 |
def read_qr_interface(img):
|
| 81 |
if img is None:
|
|
|
|
| 2 |
import cv2
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
+
# Function to generate a QR code and return base64 string
|
| 11 |
def generate_qr(data):
|
| 12 |
qr = qrcode.QRCode(
|
| 13 |
version=1,
|
|
|
|
| 19 |
qr.make(fit=True)
|
| 20 |
img = qr.make_image(fill="black", back_color="white")
|
| 21 |
|
| 22 |
+
# Encode the image as a base64 string
|
| 23 |
+
buffered = io.BytesIO()
|
| 24 |
+
img.save(buffered, format="PNG")
|
| 25 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 26 |
+
return img_base64 # Return the base64 string
|
| 27 |
|
| 28 |
|
| 29 |
# Function to read a QR code
|
|
|
|
| 54 |
|
| 55 |
with gr.Row():
|
| 56 |
qr_image = gr.Image(label="Generated QR Code")
|
| 57 |
+
qr_base64 = gr.Textbox(label="Base64 Encoded String")
|
| 58 |
|
| 59 |
def generate_qr_interface(data):
|
| 60 |
if not data.strip():
|
| 61 |
raise ValueError("Input text cannot be empty!")
|
| 62 |
+
img_base64 = generate_qr(data)
|
| 63 |
+
return f"data:image/png;base64,{img_base64}", img_base64
|
|
|
|
| 64 |
|
| 65 |
generate_button.click(
|
| 66 |
generate_qr_interface,
|
| 67 |
inputs=data_input,
|
| 68 |
+
outputs=[qr_image, qr_base64],
|
| 69 |
)
|
| 70 |
|
| 71 |
# Tab for reading QR codes
|
|
|
|
| 74 |
image_input = gr.Image(type="pil", label="Upload QR Code Image")
|
| 75 |
decode_button = gr.Button("Decode QR Code")
|
| 76 |
|
| 77 |
+
decoded_data = gr.Textbox(label="Decoded Data", interactive=True)
|
| 78 |
|
| 79 |
def read_qr_interface(img):
|
| 80 |
if img is None:
|