Adityadn commited on
Commit
bcb8d08
·
verified ·
1 Parent(s): e1f3ebd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -6
app.py CHANGED
@@ -1,10 +1,11 @@
1
  import qrcode
 
2
  import base64
3
  import gradio as gr
4
 
5
  # Function to process text and generate QR code
6
  def txt2qrcode(text: str):
7
- # Encode the text as URL-safe base64 or URL encoding
8
  encoded_text = base64.b64encode(text.encode('utf-8')).decode('utf-8') # URL encoding the input text
9
 
10
  # Construct the final URL to embed into the QR code
@@ -23,17 +24,46 @@ def txt2qrcode(text: str):
23
  # Create an image for the QR code
24
  img_qr = qr.make_image(fill='black', back_color='white')
25
 
26
- # Save the QR code image and return the path
27
- output_path = "qr_code.png"
28
- img_qr.save(output_path)
 
 
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  return output_path
31
 
 
 
 
 
 
 
 
 
 
32
  # Gradio Interface for the QR code generation
33
  def qr_code_interface(text: str):
34
  result = txt2qrcode(text)
35
  return result
36
 
 
 
 
 
37
  # Create a Gradio interface
38
  interface = gr.Interface(
39
  fn=qr_code_interface,
@@ -43,5 +73,14 @@ interface = gr.Interface(
43
  css="footer {visibility: hidden}"
44
  )
45
 
46
- # Launch the Gradio app
47
- interface.launch()
 
 
 
 
 
 
 
 
 
 
1
  import qrcode
2
+ from PIL import Image
3
  import base64
4
  import gradio as gr
5
 
6
  # Function to process text and generate QR code
7
  def txt2qrcode(text: str):
8
+ # Encode the text as URL-safe base64
9
  encoded_text = base64.b64encode(text.encode('utf-8')).decode('utf-8') # URL encoding the input text
10
 
11
  # Construct the final URL to embed into the QR code
 
24
  # Create an image for the QR code
25
  img_qr = qr.make_image(fill='black', back_color='white')
26
 
27
+ # Optionally, add a logo to the center of the QR code
28
+ logo = Image.open("logo.png") # Replace with your logo path
29
+ logo = logo.convert("RGBA")
30
+
31
+ qr_width, qr_height = img_qr.size
32
+ logo_width, logo_height = logo.size
33
 
34
+ # Resize logo to fit in the QR code
35
+ logo = logo.resize((qr_width // 5, qr_height // 5)) # Resize to be 20% of the QR code size
36
+
37
+ # Calculate position to place logo at the center
38
+ logo_position = ((qr_width - logo_width) // 2, (qr_height - logo_height) // 2)
39
+
40
+ # Paste the logo onto the QR code image
41
+ img_qr.paste(logo, logo_position, logo)
42
+
43
+ # Save the QR code image
44
+ output_path = "qr_code_with_logo.png"
45
+ img_qr.save(output_path)
46
+
47
  return output_path
48
 
49
+ # Function to process a file (e.g., text file) and generate QR code
50
+ def file2qrcode(file: str):
51
+ # Read the content of the file
52
+ with open(file, 'r') as f:
53
+ text = f.read()
54
+
55
+ # Generate QR code with the file's text content
56
+ return txt2qrcode(text)
57
+
58
  # Gradio Interface for the QR code generation
59
  def qr_code_interface(text: str):
60
  result = txt2qrcode(text)
61
  return result
62
 
63
+ def qr_code_file_interface(file: str):
64
+ result = file2qrcode(file)
65
+ return result
66
+
67
  # Create a Gradio interface
68
  interface = gr.Interface(
69
  fn=qr_code_interface,
 
73
  css="footer {visibility: hidden}"
74
  )
75
 
76
+ # Create a second Gradio interface for file input
77
+ file_interface = gr.Interface(
78
+ fn=qr_code_file_interface,
79
+ inputs=gr.File(label="Upload Text File"),
80
+ outputs=gr.Image(type="filepath", label="Generated QR Code from File"),
81
+ description="Upload a text file, and generate a QR code based on the file's content."
82
+ )
83
+
84
+ # Launch the Gradio apps
85
+ interface.launch(share=True)
86
+ file_interface.launch(share=True)