CultriX commited on
Commit
784d954
·
verified ·
1 Parent(s): 9f6b576

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -34
app.py CHANGED
@@ -3,9 +3,9 @@ import cv2
3
  from PIL import Image
4
  import gradio as gr
5
  import tempfile
6
- import os
7
  import numpy as np
8
 
 
9
  # Function to generate a QR code
10
  def generate_qr(data):
11
  qr = qrcode.QRCode(
@@ -18,18 +18,19 @@ def generate_qr(data):
18
  qr.make(fit=True)
19
  img = qr.make_image(fill="black", back_color="white")
20
 
21
- # Save to a temporary file and return the file path
22
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
23
  img.save(temp_file.name, format="PNG")
24
- temp_file.close() # Close the file to flush contents to disk
25
- return temp_file.name # Return the file path
 
26
 
27
- # Function to read a QR code using OpenCV
28
  def read_qr(img):
29
  # Convert PIL image to a NumPy array
30
  img = np.array(img)
31
 
32
- # Convert RGB to BGR format as OpenCV expects BGR
33
  if img.ndim == 3:
34
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
35
 
@@ -38,58 +39,58 @@ def read_qr(img):
38
  data, _, _ = detector.detectAndDecode(img)
39
  return data if data else "No QR code found."
40
 
41
- # Gradio interface with functional buttons
 
42
  def create_gradio_interface():
43
- # QR Code Generator with Downloadable File
44
- def generate_qr_with_download(data):
45
- qr_file = generate_qr(data) # Generate QR code file
46
- return qr_file # Return file directly as downloadable output
47
 
48
- # QR Code Reader with Copy-to-Clipboard functionality
49
- def read_qr_with_copy(img):
50
- decoded_data = read_qr(img) # Decode QR code
51
- return decoded_data, decoded_data # Return decoded text for display and copy
52
 
53
- # Generator Interface
54
  generate_interface = gr.Interface(
55
- fn=generate_qr_with_download,
56
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
57
- outputs=gr.File(label="Download Generated QR Code"), # File download
 
 
 
58
  title="Generate QR Code",
59
  description="Quickly create a QR code from any text or URL.",
60
  )
61
 
62
- # Reader Interface
63
  read_interface = gr.Interface(
64
- fn=read_qr_with_copy,
65
  inputs=gr.Image(type="pil", label="Upload QR Code Image"),
66
- outputs=[
67
- gr.Textbox(label="Decoded Data"), # Decoded QR code text
68
- gr.Textbox(visible=False), # Invisible output for clipboard content
69
- ],
70
  title="Read QR Code",
71
  description="Upload an image with a QR code to decode the embedded data.",
72
- live=False,
73
  )
74
 
75
- # Add clipboard copy functionality using Gradio button callbacks
76
- def on_copy(decoded_text):
77
- return gr.Textbox.update(value=decoded_text) # Update clipboard content
78
-
79
- # Custom UI with clipboard copy button
80
  with gr.Blocks() as demo:
81
  gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
82
  with gr.Tab("Generate QR Code"):
83
  generate_interface.render()
84
  with gr.Tab("Read QR Code"):
85
  with gr.Row():
86
- qr_text = gr.Textbox(label="Decoded Data")
87
- qr_clipboard = gr.Textbox(visible=False) # Clipboard value
88
- copy_button = gr.Button("Copy to Clipboard")
89
  read_interface.render()
90
- copy_button.click(on_copy, qr_text, qr_clipboard)
 
 
 
 
91
 
92
  demo.launch(share=True)
93
 
 
94
  # Run the Gradio interface
95
  create_gradio_interface()
 
3
  from PIL import Image
4
  import gradio as gr
5
  import tempfile
 
6
  import numpy as np
7
 
8
+
9
  # Function to generate a QR code
10
  def generate_qr(data):
11
  qr = qrcode.QRCode(
 
18
  qr.make(fit=True)
19
  img = qr.make_image(fill="black", back_color="white")
20
 
21
+ # Save to a temporary file
22
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
23
  img.save(temp_file.name, format="PNG")
24
+ temp_file.close() # Ensure file is written to disk
25
+ return temp_file.name, img # Return the file path and the PIL image
26
+
27
 
28
+ # Function to read a QR code
29
  def read_qr(img):
30
  # Convert PIL image to a NumPy array
31
  img = np.array(img)
32
 
33
+ # Convert RGB to BGR as OpenCV expects
34
  if img.ndim == 3:
35
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
36
 
 
39
  data, _, _ = detector.detectAndDecode(img)
40
  return data if data else "No QR code found."
41
 
42
+
43
+ # Gradio Interface
44
  def create_gradio_interface():
45
+ # QR Code Generator with Display and Downloadable Link
46
+ def generate_qr_interface(data):
47
+ qr_file, qr_image = generate_qr(data)
48
+ return qr_image, qr_file # Show image and provide download link
49
 
50
+ # QR Code Reader with Copy-to-Clipboard Button
51
+ def read_qr_interface(img):
52
+ decoded_data = read_qr(img)
53
+ return decoded_data # Return decoded text
54
 
55
+ # QR Code Generator Tab
56
  generate_interface = gr.Interface(
57
+ fn=generate_qr_interface,
58
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
59
+ outputs=[
60
+ gr.Image(label="Generated QR Code"), # Display the QR code image
61
+ gr.File(label="Download QR Code"), # Downloadable link for the QR code file
62
+ ],
63
  title="Generate QR Code",
64
  description="Quickly create a QR code from any text or URL.",
65
  )
66
 
67
+ # QR Code Reader Tab
68
  read_interface = gr.Interface(
69
+ fn=read_qr_interface,
70
  inputs=gr.Image(type="pil", label="Upload QR Code Image"),
71
+ outputs=gr.Textbox(label="Decoded Data"),
 
 
 
72
  title="Read QR Code",
73
  description="Upload an image with a QR code to decode the embedded data.",
 
74
  )
75
 
76
+ # Clipboard Functionality for "Read QR Code" Tab
 
 
 
 
77
  with gr.Blocks() as demo:
78
  gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
79
  with gr.Tab("Generate QR Code"):
80
  generate_interface.render()
81
  with gr.Tab("Read QR Code"):
82
  with gr.Row():
83
+ qr_text = gr.Textbox(label="Decoded Data") # Single box for decoded data
84
+ copy_button = gr.Button("Copy to Clipboard")
 
85
  read_interface.render()
86
+ copy_button.click(
87
+ lambda text: gr.Textbox.update(value="Copied to Clipboard!"),
88
+ qr_text,
89
+ qr_text,
90
+ )
91
 
92
  demo.launch(share=True)
93
 
94
+
95
  # Run the Gradio interface
96
  create_gradio_interface()