Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,97 @@
|
|
1 |
-
import
|
2 |
-
import gradio as gr
|
3 |
from PIL import Image
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import qrcode
|
|
|
2 |
from PIL import Image
|
3 |
+
import gradio as gr
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
+
import numpy as np
|
7 |
+
import cv2
|
8 |
+
import tempfile
|
9 |
+
|
10 |
+
|
11 |
+
# QR kod oluşturma işlevi
|
12 |
+
def generate_qr(data):
|
13 |
+
qr = qrcode.QRCode(
|
14 |
+
version=1,
|
15 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
16 |
+
box_size=10,
|
17 |
+
border=4,
|
18 |
+
)
|
19 |
+
qr.add_data(data)
|
20 |
+
qr.make(fit=True)
|
21 |
+
img = qr.make_image(fill="black", back_color="white")
|
22 |
+
|
23 |
+
# Base64 kodlama
|
24 |
+
buffered = io.BytesIO()
|
25 |
+
img.save(buffered, format="PNG")
|
26 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
27 |
+
|
28 |
+
# PNG olarak geçici kaydetme
|
29 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
30 |
+
img.save(temp_file.name, format="PNG")
|
31 |
+
temp_file.close()
|
32 |
+
|
33 |
+
return f"data:image/png;base64,{img_base64}", temp_file.name, img_base64
|
34 |
+
|
35 |
+
|
36 |
+
# QR kod çözme işlevi
|
37 |
+
def decode_qr(img):
|
38 |
+
if img is None:
|
39 |
+
return "No image uploaded."
|
40 |
+
img_array = np.array(img)
|
41 |
+
|
42 |
+
if img_array.ndim == 3:
|
43 |
+
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
44 |
+
|
45 |
+
detector = cv2.QRCodeDetector()
|
46 |
+
data, _, _ = detector.detectAndDecode(img_array)
|
47 |
+
return data if data else "No QR code found."
|
48 |
+
|
49 |
+
|
50 |
+
# Gradio arayüzü oluşturma
|
51 |
+
def create_gradio_interface():
|
52 |
+
with gr.Blocks() as demo:
|
53 |
+
gr.Markdown("## QR Code Generator and Decoder")
|
54 |
+
|
55 |
+
# QR kod oluşturma sekmesi
|
56 |
+
with gr.Tab("Generate QR Code"):
|
57 |
+
data_input = gr.Textbox(placeholder="Enter text or URL to encode", label="Input Data")
|
58 |
+
generate_button = gr.Button("Generate QR Code")
|
59 |
+
qr_code_html = gr.HTML(label="Generated QR Code (Base64 Embedded)")
|
60 |
+
qr_png_file = gr.File(label="Download QR Code (PNG)")
|
61 |
+
qr_base64_file = gr.File(label="Download Base64 (TXT)")
|
62 |
+
|
63 |
+
def generate_qr_interface(data):
|
64 |
+
if not data.strip():
|
65 |
+
raise ValueError("Input text cannot be empty!")
|
66 |
+
img_base64, png_path, base64_str = generate_qr(data)
|
67 |
+
|
68 |
+
base64_txt_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
|
69 |
+
with open(base64_txt_path.name, "w") as f:
|
70 |
+
f.write(base64_str)
|
71 |
+
|
72 |
+
html_content = f'<img src="{img_base64}" alt="QR Code" style="max-width:300px;">'
|
73 |
+
return html_content, png_path, base64_txt_path.name
|
74 |
+
|
75 |
+
generate_button.click(
|
76 |
+
generate_qr_interface,
|
77 |
+
inputs=data_input,
|
78 |
+
outputs=[qr_code_html, qr_png_file, qr_base64_file],
|
79 |
+
)
|
80 |
+
|
81 |
+
# QR kod çözme sekmesi
|
82 |
+
with gr.Tab("Decode QR Code"):
|
83 |
+
image_input = gr.Image(type="pil", label="Upload QR Code Image")
|
84 |
+
decode_button = gr.Button("Decode QR Code")
|
85 |
+
decoded_text = gr.Textbox(label="Decoded Text", interactive=True)
|
86 |
+
|
87 |
+
decode_button.click(
|
88 |
+
decode_qr,
|
89 |
+
inputs=image_input,
|
90 |
+
outputs=decoded_text,
|
91 |
+
)
|
92 |
+
|
93 |
+
demo.launch(share=True)
|
94 |
+
|
95 |
+
|
96 |
+
# Arayüzü çalıştırma
|
97 |
+
create_gradio_interface()
|