CultriX commited on
Commit
352e30c
1 Parent(s): 2cad3b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -33
app.py CHANGED
@@ -1,13 +1,21 @@
1
- import qrcode
 
2
  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(
12
  version=1,
13
  error_correction=qrcode.constants.ERROR_CORRECT_L,
@@ -19,59 +27,82 @@ def generate_qr(data):
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
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
 
36
  # Initialize OpenCV QR code detector
37
  detector = cv2.QRCodeDetector()
38
- data, _, _ = detector.detectAndDecode(img)
39
-
40
  return data if data else "No QR code found."
41
 
42
- # Custom CSS styling as HTML for dark mode
43
- custom_css = """
44
- <style>
45
- body {background-color: #1e1e2f; font-family: Arial, sans-serif; color: #e0e0e0;}
46
- .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);}
47
- h1, h2 {text-align: center; color: #f0f0f0; font-weight: 600;}
48
- .gr-button {background-color: #3a3a4f; color: #e0e0e0; padding: 10px 20px; border-radius: 5px; font-size: 15px; border: none;}
49
- .gr-button:hover {background-color: #505068;}
50
- input, textarea, .gr-box {background-color: #3a3a4f; border: 1px solid #555; border-radius: 5px; padding: 10px; font-size: 14px; color: #e0e0e0;}
51
- .gr-box:hover, input:hover, textarea:hover {border-color: #777;}
52
- </style>
53
- """
 
 
 
 
 
 
54
 
55
- # Gradio interface for generating and reading QR codes
56
  def create_gradio_interface():
 
 
 
57
  # QR Code Generator Interface
58
  generate_interface = gr.Interface(
59
- fn=generate_qr,
60
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
61
  outputs=gr.Image(label="Generated QR Code"),
62
  title="Generate QR Code",
63
  description="Quickly create a QR code from any text or URL.",
64
  )
65
 
 
 
 
 
66
  # QR Code Reader Interface
67
  read_interface = gr.Interface(
68
- fn=read_qr,
69
  inputs=gr.Image(type="pil", label="Upload QR Code Image"),
70
  outputs=gr.Textbox(label="Decoded Data"),
71
  title="Read QR Code",
72
  description="Upload an image with a QR code to decode the embedded data.",
73
  )
74
 
 
 
 
 
75
  # Combine interfaces into a single tabbed layout
76
  interface = gr.TabbedInterface(
77
  [generate_interface, read_interface],
@@ -85,5 +116,16 @@ def create_gradio_interface():
85
 
86
  demo.launch(share=True)
87
 
88
- # Run the Gradio interface
89
- create_gradio_interface()
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import tempfile
3
  import cv2
4
+ import numpy as np
5
  from PIL import Image
6
  import gradio as gr
7
+ import pyperclip
8
+
9
+ def generate_qr_code(data):
10
+ """
11
+ Generate a QR code from the given data.
12
+
13
+ Args:
14
+ data (str): The data to encode in the QR code.
15
 
16
+ Returns:
17
+ str: The path to the generated QR code image.
18
+ """
19
  qr = qrcode.QRCode(
20
  version=1,
21
  error_correction=qrcode.constants.ERROR_CORRECT_L,
 
27
  img = qr.make_image(fill="black", back_color="white")
28
 
29
  # Save to a temporary file and return the file path
30
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
31
+ img.save(temp_file.name, format="PNG")
 
32
  return temp_file.name
33
 
34
+ def read_qr_code(img):
35
+ """
36
+ Read a QR code from the given image.
37
+
38
+ Args:
39
+ img (PIL.Image): The image containing the QR code.
40
+
41
+ Returns:
42
+ str: The decoded data from the QR code.
43
+ """
44
  # Convert PIL image to a NumPy array
45
+ img_array = np.array(img)
46
 
47
  # Convert RGB to BGR format as OpenCV expects BGR
48
+ if img_array.ndim == 3:
49
+ img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
50
 
51
  # Initialize OpenCV QR code detector
52
  detector = cv2.QRCodeDetector()
53
+ data, _, _ = detector.detectAndDecode(img_array)
54
+
55
  return data if data else "No QR code found."
56
 
57
+ def download_qr_code(qr_image):
58
+ """
59
+ Download the generated QR code image.
60
+
61
+ Args:
62
+ qr_image (str): The path to the generated QR code image.
63
+ """
64
+ # Implement download logic here
65
+ pass
66
+
67
+ def copy_to_clipboard(decoded_text):
68
+ """
69
+ Copy the decoded text to the clipboard.
70
+
71
+ Args:
72
+ decoded_text (str): The decoded text to copy.
73
+ """
74
+ pyperclip.copy(decoded_text)
75
 
 
76
  def create_gradio_interface():
77
+ """
78
+ Create the Gradio interface for generating and reading QR codes.
79
+ """
80
  # QR Code Generator Interface
81
  generate_interface = gr.Interface(
82
+ fn=generate_qr_code,
83
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
84
  outputs=gr.Image(label="Generated QR Code"),
85
  title="Generate QR Code",
86
  description="Quickly create a QR code from any text or URL.",
87
  )
88
 
89
+ # Add download button to generated QR code
90
+ download_button = gr.Button("Download QR Code")
91
+ download_button.click(fn=lambda x: download_qr_code(x), inputs="Generated QR Code", outputs=None)
92
+
93
  # QR Code Reader Interface
94
  read_interface = gr.Interface(
95
+ fn=read_qr_code,
96
  inputs=gr.Image(type="pil", label="Upload QR Code Image"),
97
  outputs=gr.Textbox(label="Decoded Data"),
98
  title="Read QR Code",
99
  description="Upload an image with a QR code to decode the embedded data.",
100
  )
101
 
102
+ # Add copy to clipboard button to decoded text
103
+ copy_button = gr.Button("Copy to Clipboard")
104
+ copy_button.click(fn=lambda x: copy_to_clipboard(x), inputs="Decoded Data", outputs=None)
105
+
106
  # Combine interfaces into a single tabbed layout
107
  interface = gr.TabbedInterface(
108
  [generate_interface, read_interface],
 
116
 
117
  demo.launch(share=True)
118
 
119
+ custom_css = """
120
+ <style>
121
+ body {background-color: #1e1e2f; font-family: Arial, sans-serif; color: #e0e0e0;}
122
+ .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);}
123
+ h1, h2 {text-align: center; color: #f0f0f0; font-weight: 600;}
124
+ .gr-button {background-color: #3a3a4f; color: #e0e0e0; padding: 10px 20px; border-radius: 5px; font-size: 15px; border: none;}
125
+ .gr-button:hover {background-color: #505068;}
126
+ input, textarea,.gr-box {background-color: #3a3a4f; border: 1px solid #555; border-radius: 5px; padding: 10px; font-size: 14px; color: #e0e0e0;}
127
+ .gr-box:hover, input:hover, textarea:hover {border-color: #777;}
128
+ </style>
129
+ """
130
+
131
+ create_gradio_interface()