CultriX commited on
Commit
c54e6c8
·
verified ·
1 Parent(s): b655508

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -2,12 +2,12 @@ import qrcode
2
  import cv2
3
  from PIL import Image
4
  import gradio as gr
5
- import tempfile
 
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
- # Save QR code image to a temporary file
23
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
24
- img.save(temp_file.name, format="PNG")
25
- temp_file.close() # Ensure the file is saved
26
- return temp_file.name # Return the file path
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
- qr_file = gr.File(label="Download QR Code")
58
 
59
  def generate_qr_interface(data):
60
  if not data.strip():
61
  raise ValueError("Input text cannot be empty!")
62
- qr_file_path = generate_qr(data)
63
- qr_image_pil = Image.open(qr_file_path)
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, qr_file],
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: