-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradio_qr_code.py
238 lines (181 loc) · 8.52 KB
/
gradio_qr_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
import qrcode
from PIL import Image
import gradio as gr
def resize_for_condition_image(input_image: Image.Image, resolution: int):
input_image = input_image.convert("RGB")
W, H = input_image.size
k = float(resolution) / min(H, W)
H *= k
W *= k
H = int(round(H / 64.0)) * 64
W = int(round(W / 64.0)) * 64
img = input_image.resize((W, H), resample=Image.LANCZOS)
return img
def build_string_vcard(name, surname, phone, email,website, position, company, city, state, country):
result = "BEGIN:VCARD\nVERSION:3.0\n"
if name is not None and name != "":
result += f"FN: {name}"
if surname is not None and surname != "":
result += f" {surname}\n"
else :
result += f"\n"
if phone is not None and phone != "":
result += f"TEL;CELL: {phone}\n"
if email is not None and email != "":
result += f"EMAIL;WORK;INTERNET: {email}\n"
if website is not None and website != "":
result += f"URL: {website}\n"
if company is not None and company != "":
result += f"ORG: {company}\n"
if position is not None and position != "":
result += f"TITLE: {position}\n"
result += f"ADR:;"
if city is not None and city != "":
result += f";{city}"
if state is not None and state != "":
result += f";{state}"
if country is not None and country != "":
result += f";{country}\n"
else:
result += f"\n"
result += f"END:VCARD"
return result
def url_qr_code(text,size):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(text)
qr.make(fit=True)
qrcode_image = qr.make_image(fill_color="black", back_color="white")
qrcode_image = resize_for_condition_image(qrcode_image, size)
return qrcode_image
def message_qr_code(text,size):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(text)
qr.make(fit=True)
qrcode_image = qr.make_image(fill_color="black", back_color="white")
qrcode_image = resize_for_condition_image(qrcode_image, size)
return qrcode_image
def sms_qr_code(number,message, size):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
text = f"SMSTO:{number}:message"
qr.add_data(text)
qr.make(fit=True)
qrcode_image = qr.make_image(fill_color="black", back_color="white")
qrcode_image = resize_for_condition_image(qrcode_image, size)
return qrcode_image
def wifi_qr_code(name,password, size):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
text = f"WIFI:S:{name};T:WPA;P:{password};;"
qr.add_data(text)
qr.make(fit=True)
qrcode_image = qr.make_image(fill_color="black", back_color="white")
qrcode_image = resize_for_condition_image(qrcode_image, size)
return qrcode_image
def email_qr_code(email,subject,message, size):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
text = f"MATMSG:TO:{email};SUB:{subject};BODY:{message};;"
qr.add_data(text)
qr.make(fit=True)
qrcode_image = qr.make_image(fill_color="black", back_color="white")
qrcode_image = resize_for_condition_image(qrcode_image, size)
return qrcode_image
def vcard_qr_code(name, surname, phone, email,website, position, company, city, state, country, size):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
text = build_string_vcard(name, surname, phone, email,website, position, company, city, state, country,)
qr.add_data(text)
qr.make(fit=True)
qrcode_image = qr.make_image(fill_color="black", back_color="white")
qrcode_image = resize_for_condition_image(qrcode_image, size)
return qrcode_image
with gr.Blocks(title="QR code generator", css="footer {visibility: hidden}") as app:
with gr.Row():
with gr.Tab("🌐 URL"):
url_text = gr.Textbox(label="Website (URL)", value="https://www.instagram.com/qrcodecustoms")
url_size = gr.Slider(label = "Height and Width", minimum=768, maximum=2048, value=768, step=1)
url_button = gr.Button("▶️ Run")
with gr.Tab("💬 Message"):
message_text = gr.TextArea(label="Text")
message_size = gr.Slider(label = "Height and Width", minimum=768, maximum=2048, value=768, step=1)
message_button = gr.Button("▶️ Run")
with gr.Tab("📱 SMS"):
sms_number = gr.Textbox(label="Number")
sms_message = gr.TextArea(label="Text")
sms_size = gr.Slider(label = "Height and Width", minimum=768, maximum=2048, value=768, step=1)
sms_button = gr.Button("▶️ Run")
with gr.Tab("📻 Wi-Fi"):
wifi_name = gr.Textbox(label="Wi-Fi name (SSID)")
wifi_password = gr.Textbox(label="Password")
wifi_size = gr.Slider(label = "Height and Width", minimum=768, maximum=2048, value=768, step=1)
wifi_button = gr.Button("▶️ Run")
with gr.Tab("📩 Email"):
email_address = gr.Textbox(label="Email")
email_subject = gr.Textbox(label="Subject")
email_message = gr.TextArea(label="Message")
email_size = gr.Slider(label = "Height and Width", minimum=768, maximum=2048, value=768, step=1)
email_button = gr.Button("▶️ Run")
with gr.Tab("🪪 VCard"):
with gr.Row():
vcard_name = gr.Textbox(label="Name")
vcard_surname = gr.Textbox(label="Surname")
with gr.Row():
vcard_phone = gr.Textbox(label="Phone number")
vcard_email = gr.Textbox(label="Email")
with gr.Row():
vcard_website = gr.Textbox(label="Website")
vcard_position = gr.Textbox(label="Job position")
with gr.Row():
vcard_company = gr.Textbox(label="Company")
vcard_city = gr.Textbox(label="City")
with gr.Row():
vcard_state = gr.Textbox(label="State/Region/Province")
vcard_country = gr.Textbox(label="Country")
vcard_size = gr.Slider(label = "Height and Width", minimum=768, maximum=2048, value=768, step=1)
vcard_button = gr.Button("▶️ Run")
output_image = gr.Image(label = "Resulting QR code image")
url_button.click(fn = url_qr_code, inputs=[url_text,url_size], outputs=[output_image])
message_button.click(fn = message_qr_code,
inputs=[message_text,message_size],
outputs=[output_image])
sms_button.click(fn = sms_qr_code,
inputs=[sms_number,sms_message, sms_size],
outputs=[output_image])
wifi_button.click(fn = wifi_qr_code,
inputs=[wifi_name,wifi_password, wifi_size],
outputs=[output_image])
email_button.click(fn = email_qr_code,
inputs=[email_address,email_subject, email_message, email_size],
outputs=[output_image])
vcard_button.click(fn=vcard_qr_code,
inputs=[vcard_name, vcard_surname, vcard_phone, vcard_email, vcard_website,
vcard_position, vcard_company,vcard_city, vcard_state, vcard_country, vcard_size],
outputs=[output_image])
app.launch()