Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from datetime import datetime
|
|
6 |
from PIL import Image, ImageDraw
|
7 |
from math import cos, sin, radians
|
8 |
|
|
|
9 |
def create_border_decoration(qr_image, decoration_prompt="flowers"):
|
10 |
# Convert QR image to RGB if it's not
|
11 |
if qr_image.mode != 'RGB':
|
@@ -49,298 +50,248 @@ def create_border_decoration(qr_image, decoration_prompt="flowers"):
|
|
49 |
|
50 |
return decorated_image
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
if border_decoration.strip(): # Only if decoration prompt is not empty
|
97 |
-
qr_img = create_border_decoration(qr_img, border_decoration)
|
98 |
-
|
99 |
-
# 파일 저장
|
100 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
101 |
-
random_id = random.randint(1000, 9999)
|
102 |
-
filename = f"qrfile/qr_{timestamp}_{random_id}.png"
|
103 |
-
|
104 |
-
# 디렉토리 확인 및 생성
|
105 |
-
os.makedirs("qrfile", exist_ok=True)
|
106 |
-
|
107 |
-
# 이미지 저장
|
108 |
-
qr_img.save(filename)
|
109 |
-
cleanup_old_files("qrfile/", max_files=100)
|
110 |
-
|
111 |
-
return filename, formatted_data
|
112 |
|
113 |
# 데이터 포맷팅 함수
|
114 |
def format_data(content, qr_type):
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
|
132 |
# 파일 정리 함수
|
133 |
def cleanup_old_files(directory, max_files):
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
def get_example_placeholder(qr_type):
|
144 |
-
examples = {
|
145 |
-
"URL": "example.com or https://example.com",
|
146 |
-
"Email": "[email protected]",
|
147 |
-
"Phone": "+1234567890",
|
148 |
-
"SMS": "+1234567890",
|
149 |
-
"WhatsApp": "Hello World!",
|
150 |
-
"Location": "37.7749,-122.4194",
|
151 |
-
"Wi-Fi": "MyWiFiNetwork",
|
152 |
-
"Text": "Any text you want to encode",
|
153 |
-
"vCard": "FN:John Doe\nTEL:+1234567890\nEMAIL:[email protected]"
|
154 |
-
}
|
155 |
-
return examples.get(qr_type, "Enter your content here...")
|
156 |
|
157 |
def format_example_text(qr_type):
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
|
171 |
-
# Gradio 인터페이스 생성
|
172 |
# Gradio 인터페이스 생성
|
173 |
def create_interface():
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
"""
|
316 |
-
### 📝 Instructions
|
317 |
-
1. Select the QR code type from the dropdown menu
|
318 |
-
2. Enter your content following the format examples shown
|
319 |
-
3. Customize the appearance using the color pickers and sliders
|
320 |
-
4. Click 'Generate QR Code' to create your custom QR code
|
321 |
-
|
322 |
-
### 💡 Tips
|
323 |
-
- Use higher error correction levels for better scan reliability
|
324 |
-
- Ensure sufficient contrast between QR code and background colors
|
325 |
-
- Keep the content concise for better readability
|
326 |
-
- Follow the format examples for best results
|
327 |
-
"""
|
328 |
-
)
|
329 |
-
|
330 |
-
return demo
|
331 |
|
332 |
if __name__ == "__main__":
|
333 |
try:
|
334 |
-
# 출력 디렉토리 생성
|
335 |
os.makedirs("qrfile", exist_ok=True)
|
336 |
-
|
337 |
-
# 인터페이스 생성 및 실행
|
338 |
demo = create_interface()
|
339 |
demo.launch(
|
340 |
-
server_name="0.0.0.0",
|
341 |
-
server_port=7860,
|
342 |
-
share=True,
|
343 |
-
debug=True
|
344 |
)
|
345 |
except Exception as e:
|
346 |
print(f"Error starting the application: {e}")
|
|
|
6 |
from PIL import Image, ImageDraw
|
7 |
from math import cos, sin, radians
|
8 |
|
9 |
+
# 테두리 장식 함수
|
10 |
def create_border_decoration(qr_image, decoration_prompt="flowers"):
|
11 |
# Convert QR image to RGB if it's not
|
12 |
if qr_image.mode != 'RGB':
|
|
|
50 |
|
51 |
return decorated_image
|
52 |
|
53 |
+
# QR 코드 생성 함수
|
54 |
+
def create_qr(content, qr_type, fill_color, back_color, box_size, border_size, error_correction, border_decoration="flowers"):
|
55 |
+
# QR 코드 데이터 포맷팅
|
56 |
+
formatted_data = format_data(content, qr_type)
|
57 |
+
|
58 |
+
# 에러 수정 레벨 설정
|
59 |
+
error_levels = {
|
60 |
+
"Low (7%)": qrcode.constants.ERROR_CORRECT_L,
|
61 |
+
"Medium (15%)": qrcode.constants.ERROR_CORRECT_M,
|
62 |
+
"Quartile (25%)": qrcode.constants.ERROR_CORRECT_Q,
|
63 |
+
"High (30%)": qrcode.constants.ERROR_CORRECT_H
|
64 |
+
}
|
65 |
+
|
66 |
+
# QR 코드 생성
|
67 |
+
qr = qrcode.QRCode(
|
68 |
+
version=1,
|
69 |
+
error_correction=error_levels[error_correction],
|
70 |
+
box_size=box_size,
|
71 |
+
border=border_size,
|
72 |
+
)
|
73 |
+
|
74 |
+
qr.add_data(formatted_data)
|
75 |
+
qr.make(fit=True)
|
76 |
+
|
77 |
+
# QR 이미지 생성
|
78 |
+
qr_img = qr.make_image(fill_color=fill_color, back_color=back_color)
|
79 |
+
|
80 |
+
# Add border decoration if specified
|
81 |
+
if border_decoration.strip(): # Only if decoration prompt is not empty
|
82 |
+
qr_img = create_border_decoration(qr_img, border_decoration)
|
83 |
+
|
84 |
+
# 파일 저장
|
85 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
86 |
+
random_id = random.randint(1000, 9999)
|
87 |
+
filename = f"qrfile/qr_{timestamp}_{random_id}.png"
|
88 |
+
|
89 |
+
# 디렉토리 확인 및 생성
|
90 |
+
os.makedirs("qrfile", exist_ok=True)
|
91 |
+
|
92 |
+
# 이미지 저장
|
93 |
+
qr_img.save(filename)
|
94 |
+
cleanup_old_files("qrfile/", max_files=100)
|
95 |
+
|
96 |
+
return filename, formatted_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
# 데이터 포맷팅 함수
|
99 |
def format_data(content, qr_type):
|
100 |
+
if not content:
|
101 |
+
return ""
|
102 |
+
|
103 |
+
format_rules = {
|
104 |
+
"URL": lambda x: f"https://{x}" if not x.startswith(('http://', 'https://')) else x,
|
105 |
+
"Email": lambda x: f"mailto:{x}",
|
106 |
+
"Phone": lambda x: f"tel:{x}",
|
107 |
+
"SMS": lambda x: f"sms:{x}",
|
108 |
+
"WhatsApp": lambda x: f"whatsapp://send?text={x}",
|
109 |
+
"Location": lambda x: f"geo:{x}",
|
110 |
+
"Wi-Fi": lambda x: f"WIFI:S:{x};;",
|
111 |
+
"Text": lambda x: x,
|
112 |
+
"vCard": lambda x: f"BEGIN:VCARD\nVERSION:3.0\n{x}\nEND:VCARD"
|
113 |
+
}
|
114 |
+
|
115 |
+
return format_rules[qr_type](content.strip())
|
116 |
|
117 |
# 파일 정리 함수
|
118 |
def cleanup_old_files(directory, max_files):
|
119 |
+
files = [f for f in os.listdir(directory) if f.endswith('.png')]
|
120 |
+
if len(files) > max_files:
|
121 |
+
files.sort(key=lambda x: os.path.getctime(os.path.join(directory, x)))
|
122 |
+
for f in files[:-max_files]:
|
123 |
+
try:
|
124 |
+
os.remove(os.path.join(directory, f))
|
125 |
+
except:
|
126 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
def format_example_text(qr_type):
|
129 |
+
examples = {
|
130 |
+
"URL": "• Direct URL: https://example.com\n• Without https: example.com",
|
131 |
+
"Email": "• Basic: [email protected]\n• With subject: [email protected]?subject=Hello",
|
132 |
+
"Phone": "• International: +1234567890\n• Local: 01012345678",
|
133 |
+
"SMS": "• Basic: +1234567890\n• With message: +1234567890?body=Hello",
|
134 |
+
"WhatsApp": "• Message: Hello World!\n• With number: +1234567890:Hello",
|
135 |
+
"Location": "• Coordinates: 37.7749,-122.4194\n• With zoom: 37.7749,-122.4194,15z",
|
136 |
+
"Wi-Fi": "• Network name only: MyWiFiNetwork\n• With password: WIFI:S:MyNetwork;P:password;;",
|
137 |
+
"Text": "• Simple text: Hello World!\n• Multiple lines: Line 1\\nLine 2",
|
138 |
+
"vCard": "• Basic:\nFN:John Doe\nTEL:+1234567890\nEMAIL:[email protected]\n• Extended:\nFN:John Doe\nTEL:+1234567890\nEMAIL:[email protected]\nADR:;;123 Street;City;State;12345;Country"
|
139 |
+
}
|
140 |
+
return examples.get(qr_type, "Enter your content here...")
|
141 |
|
|
|
142 |
# Gradio 인터페이스 생성
|
143 |
def create_interface():
|
144 |
+
theme = gr.themes.Soft(
|
145 |
+
primary_hue="blue",
|
146 |
+
secondary_hue="indigo",
|
147 |
+
).set(
|
148 |
+
body_background_fill="*neutral_50",
|
149 |
+
block_background_fill="*neutral_100",
|
150 |
+
button_primary_background_fill="*primary_500",
|
151 |
+
)
|
152 |
+
|
153 |
+
with gr.Blocks(theme=theme, title="QR Canvas") as demo:
|
154 |
+
gr.Markdown(
|
155 |
+
"""
|
156 |
+
# 🎯 QR CANVAS
|
157 |
+
Create customized QR codes for various purposes with professional styling options.
|
158 |
+
"""
|
159 |
+
)
|
160 |
+
|
161 |
+
gr.HTML("""<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fginipick-QR-Canvas.hf.space">
|
162 |
+
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fginipick-QR-Canvas.hf.space&countColor=%23263759" />
|
163 |
+
</a>""")
|
164 |
+
|
165 |
+
with gr.Row():
|
166 |
+
with gr.Column(scale=2):
|
167 |
+
qr_type = gr.Dropdown(
|
168 |
+
choices=["URL", "Email", "Phone", "SMS", "WhatsApp", "Location", "Wi-Fi", "Text", "vCard"],
|
169 |
+
value="URL",
|
170 |
+
label="QR Code Type"
|
171 |
+
)
|
172 |
+
|
173 |
+
content = gr.Textbox(
|
174 |
+
label="Content",
|
175 |
+
placeholder="Enter your content here...",
|
176 |
+
lines=3
|
177 |
+
)
|
178 |
+
|
179 |
+
example_format = gr.Textbox(
|
180 |
+
value=format_example_text("URL"),
|
181 |
+
label="Format Examples",
|
182 |
+
interactive=False,
|
183 |
+
lines=6
|
184 |
+
)
|
185 |
+
|
186 |
+
with gr.Row():
|
187 |
+
fill_color = gr.ColorPicker(
|
188 |
+
label="QR Code Color",
|
189 |
+
value="#000000"
|
190 |
+
)
|
191 |
+
back_color = gr.ColorPicker(
|
192 |
+
label="Background Color",
|
193 |
+
value="#FFFFFF"
|
194 |
+
)
|
195 |
+
|
196 |
+
with gr.Row():
|
197 |
+
box_size = gr.Slider(
|
198 |
+
minimum=1,
|
199 |
+
maximum=20,
|
200 |
+
value=10,
|
201 |
+
step=1,
|
202 |
+
label="QR Code Size"
|
203 |
+
)
|
204 |
+
border_size = gr.Slider(
|
205 |
+
minimum=0,
|
206 |
+
maximum=10,
|
207 |
+
value=4,
|
208 |
+
step=1,
|
209 |
+
label="Border Size"
|
210 |
+
)
|
211 |
+
|
212 |
+
error_correction = gr.Dropdown(
|
213 |
+
choices=[
|
214 |
+
"Low (7%)",
|
215 |
+
"Medium (15%)",
|
216 |
+
"Quartile (25%)",
|
217 |
+
"High (30%)"
|
218 |
+
],
|
219 |
+
value="Medium (15%)",
|
220 |
+
label="Error Correction Level"
|
221 |
+
)
|
222 |
+
|
223 |
+
border_decoration = gr.Textbox(
|
224 |
+
label="Border Decoration Prompt",
|
225 |
+
placeholder="Enter decoration style (e.g., flowers, simple, modern)",
|
226 |
+
value="flowers"
|
227 |
+
)
|
228 |
+
|
229 |
+
generate_btn = gr.Button(
|
230 |
+
"Generate QR Code",
|
231 |
+
variant="primary"
|
232 |
+
)
|
233 |
+
|
234 |
+
with gr.Column(scale=1):
|
235 |
+
output_image = gr.Image(
|
236 |
+
label="Generated QR Code",
|
237 |
+
type="filepath"
|
238 |
+
)
|
239 |
+
output_data = gr.Textbox(
|
240 |
+
label="Formatted Data",
|
241 |
+
interactive=False
|
242 |
+
)
|
243 |
+
|
244 |
+
def update_example(qr_type):
|
245 |
+
return format_example_text(qr_type)
|
246 |
+
|
247 |
+
qr_type.change(
|
248 |
+
fn=update_example,
|
249 |
+
inputs=[qr_type],
|
250 |
+
outputs=example_format
|
251 |
+
)
|
252 |
+
|
253 |
+
generate_btn.click(
|
254 |
+
fn=create_qr,
|
255 |
+
inputs=[
|
256 |
+
content,
|
257 |
+
qr_type,
|
258 |
+
fill_color,
|
259 |
+
back_color,
|
260 |
+
box_size,
|
261 |
+
border_size,
|
262 |
+
error_correction,
|
263 |
+
border_decoration
|
264 |
+
],
|
265 |
+
outputs=[output_image, output_data]
|
266 |
+
)
|
267 |
+
|
268 |
+
gr.Markdown(
|
269 |
+
"""
|
270 |
+
### 📝 Instructions
|
271 |
+
1. Select the QR code type from the dropdown menu
|
272 |
+
2. Enter your content following the format examples shown
|
273 |
+
3. Customize the appearance using the color pickers and sliders
|
274 |
+
4. Click 'Generate QR Code' to create your custom QR code
|
275 |
+
|
276 |
+
### 💡 Tips
|
277 |
+
- Use higher error correction levels for better scan reliability
|
278 |
+
- Ensure sufficient contrast between QR code and background colors
|
279 |
+
- Keep the content concise for better readability
|
280 |
+
- Follow the format examples for best results
|
281 |
+
"""
|
282 |
+
)
|
283 |
+
|
284 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
285 |
|
286 |
if __name__ == "__main__":
|
287 |
try:
|
|
|
288 |
os.makedirs("qrfile", exist_ok=True)
|
|
|
|
|
289 |
demo = create_interface()
|
290 |
demo.launch(
|
291 |
+
server_name="0.0.0.0",
|
292 |
+
server_port=7860,
|
293 |
+
share=True,
|
294 |
+
debug=True
|
295 |
)
|
296 |
except Exception as e:
|
297 |
print(f"Error starting the application: {e}")
|