Simonlob commited on
Commit
cfed1c4
·
verified ·
1 Parent(s): 2925665

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -4
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import qrcode
3
  import qrcode.constants
4
  from PIL import Image
 
5
 
6
  def get_qr_image(data: str, fill_color: str = None, back_color: str = None):
7
  """
@@ -54,7 +55,14 @@ def generate_qr(url, fill_color=None, back_color=None):
54
  back_color = back_color.strip() if back_color else 'white'
55
 
56
  # Generate QR code with optional color parameters
57
- return get_qr_image(url, fill_color, back_color)
 
 
 
 
 
 
 
58
 
59
  except Exception as e:
60
  # Error handling for Gradio
@@ -69,11 +77,102 @@ iface = gr.Interface(
69
  gr.Textbox(label="Fill Color (optional)", placeholder="e.g., 'blue', '#FF0000'"),
70
  gr.Textbox(label="Background Color (optional)", placeholder="e.g., 'white', '#FFFFFF'")
71
  ],
72
- outputs=gr.Image(label="QR Code"),
73
  title="QR Code Generator",
74
- description="Generate a QR code from a URL or text with optional color customization."
 
 
 
 
 
75
  )
76
 
77
  # Launch the app
78
  if __name__ == "__main__":
79
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import qrcode
3
  import qrcode.constants
4
  from PIL import Image
5
+ import io
6
 
7
  def get_qr_image(data: str, fill_color: str = None, back_color: str = None):
8
  """
 
55
  back_color = back_color.strip() if back_color else 'white'
56
 
57
  # Generate QR code with optional color parameters
58
+ img = get_qr_image(url, fill_color, back_color)
59
+
60
+ # Save to a bytes buffer in PNG format
61
+ buffer = io.BytesIO()
62
+ img.save(buffer, format='PNG')
63
+ buffer.seek(0)
64
+
65
+ return buffer
66
 
67
  except Exception as e:
68
  # Error handling for Gradio
 
77
  gr.Textbox(label="Fill Color (optional)", placeholder="e.g., 'blue', '#FF0000'"),
78
  gr.Textbox(label="Background Color (optional)", placeholder="e.g., 'white', '#FFFFFF'")
79
  ],
80
+ outputs=gr.Image(label="QR Code", type="file"),
81
  title="QR Code Generator",
82
+ description="Generate a QR code from a URL or text with optional color customization.",
83
+ # Optional: Specify file type
84
+ examples=[
85
+ ["https://example.com"],
86
+ ["Hello World", "blue", "white"]
87
+ ]
88
  )
89
 
90
  # Launch the app
91
  if __name__ == "__main__":
92
+ iface.launch()
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+ # import gradio as gr
101
+ # import qrcode
102
+ # import qrcode.constants
103
+ # from PIL import Image
104
+
105
+ # def get_qr_image(data: str, fill_color: str = None, back_color: str = None):
106
+ # """
107
+ # Generate a QR code image from the given data and optional color parameters.
108
+
109
+ # Args:
110
+ # data (str): The data to encode in the QR code
111
+ # fill_color (str, optional): Color of the QR code modules (default: black)
112
+ # back_color (str, optional): Background color of the QR code (default: white)
113
+
114
+ # Returns:
115
+ # PIL.Image: Generated QR code image
116
+ # """
117
+ # qr = qrcode.QRCode(
118
+ # version=1,
119
+ # error_correction=qrcode.constants.ERROR_CORRECT_H,
120
+ # box_size=10,
121
+ # border=4,
122
+ # )
123
+
124
+ # qr.add_data(data)
125
+ # qr.make(fit=True)
126
+
127
+ # # Explicitly create a PIL Image and convert to RGB mode
128
+ # img = qr.make_image(fill_color=fill_color, back_color=back_color)
129
+ # img = img.convert('RGB')
130
+
131
+ # return img
132
+
133
+ # def generate_qr(url, fill_color=None, back_color=None):
134
+ # """
135
+ # Wrapper function for Gradio interface to generate QR code.
136
+ # Handles potential errors and provides default behavior.
137
+
138
+ # Args:
139
+ # url (str): URL or data to encode
140
+ # fill_color (str, optional): Color of QR code modules
141
+ # back_color (str, optional): Background color of QR code
142
+
143
+ # Returns:
144
+ # PIL.Image: Generated QR code image
145
+ # """
146
+ # try:
147
+ # # Validate URL/data is not empty
148
+ # if not url:
149
+ # raise ValueError("URL or data cannot be empty")
150
+
151
+ # # Convert empty strings to None
152
+ # fill_color = fill_color.strip() if fill_color else 'black'
153
+ # back_color = back_color.strip() if back_color else 'white'
154
+
155
+ # # Generate QR code with optional color parameters
156
+ # return get_qr_image(url, fill_color, back_color)
157
+
158
+ # except Exception as e:
159
+ # # Error handling for Gradio
160
+ # gr.Warning(f"Error generating QR code: {str(e)}")
161
+ # return None
162
+
163
+ # # Create Gradio interface
164
+ # iface = gr.Interface(
165
+ # fn=generate_qr,
166
+ # inputs=[
167
+ # gr.Textbox(label="URL or Data"),
168
+ # gr.Textbox(label="Fill Color (optional)", placeholder="e.g., 'blue', '#FF0000'"),
169
+ # gr.Textbox(label="Background Color (optional)", placeholder="e.g., 'white', '#FFFFFF'")
170
+ # ],
171
+ # outputs=gr.Image(label="QR Code"),
172
+ # title="QR Code Generator",
173
+ # description="Generate a QR code from a URL or text with optional color customization."
174
+ # )
175
+
176
+ # # Launch the app
177
+ # if __name__ == "__main__":
178
+ # iface.launch()