TS447 commited on
Commit
f1aee34
·
verified ·
1 Parent(s): fafd897

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from rembg import remove
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image
6
+ import os
7
+ import subprocess
8
+
9
+ # --- 1. Processing Logic ---
10
+ def convert_to_vector(image, detail_threshold, smoothness):
11
+ if image is None:
12
+ return None, None
13
+
14
+ # Step A: AI Background Removal (Cleaning)
15
+ # Photo mein se background uda do taaki sirf Jali bache
16
+ print("Removing Background...")
17
+ try:
18
+ # Convert PIL to simple RGB if needed
19
+ image = image.convert("RGB")
20
+ # Rembg magic
21
+ output_image = remove(image) # Returns RGBA
22
+ except Exception as e:
23
+ print(f"AI Error: {e}")
24
+ output_image = image # Fallback
25
+
26
+ # Step B: Prepare for Tracing (Black & White conversion)
27
+ # Alpha channel (transparency) ko mask banao
28
+ img_np = np.array(output_image)
29
+
30
+ # Agar transparent hai (PNG), to Alpha use karo, nahi to Grayscale
31
+ if img_np.shape[2] == 4:
32
+ alpha = img_np[:, :, 3]
33
+ # Thresholding based on slider
34
+ _, binary = cv2.threshold(alpha, detail_threshold, 255, cv2.THRESH_BINARY)
35
+ else:
36
+ gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)
37
+ _, binary = cv2.threshold(gray, detail_threshold, 255, cv2.THRESH_BINARY_INV)
38
+
39
+ # Save temp BMP (Potrace needs BMP)
40
+ temp_bmp = "temp_trace.bmp"
41
+ cv2.imwrite(temp_bmp, binary)
42
+
43
+ # Step C: Vectorize using Potrace
44
+ # Command line magic: BMP -> SVG
45
+ # -t = turdsize (speckle noise remove karne ke liye)
46
+ # -a = alphamax (curves kitne smooth honge)
47
+ output_svg = "ts_vector_design.svg"
48
+
49
+ # Smoothness slider logic (0.5 to 1.3)
50
+ corner_policy = "1" # Turn off optimization
51
+
52
+ cmd = [
53
+ "potrace", temp_bmp,
54
+ "-s", # Output SVG
55
+ "-o", output_svg,
56
+ "-t", "10", # Chote daag dhabbe ignore karo
57
+ "-a", str(smoothness), # Smoothness control
58
+ "--opaque" # Solid fill
59
+ ]
60
+
61
+ subprocess.run(cmd, check=True)
62
+
63
+ # Return SVG path and Preview Image (Black & White version)
64
+ return output_svg, Image.fromarray(binary)
65
+
66
+ # --- 2. Premium TS Interface ---
67
+ custom_css = """
68
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;900&display=swap');
69
+
70
+ body, .gradio-container {
71
+ font-family: 'Inter', sans-serif !important;
72
+ background: #0f172a !important; /* Rich Dark Blue */
73
+ color: #e2e8f0 !important;
74
+ }
75
+
76
+ #main_card {
77
+ background: rgba(30, 41, 59, 0.7);
78
+ backdrop-filter: blur(20px);
79
+ border: 1px solid rgba(255, 255, 255, 0.1);
80
+ border-radius: 24px;
81
+ padding: 30px;
82
+ box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
83
+ max-width: 1000px;
84
+ margin: 30px auto;
85
+ }
86
+
87
+ #logo_text h1 {
88
+ background: linear-gradient(to right, #fbbf24, #d97706); /* Golden Gradient for CNC */
89
+ -webkit-background-clip: text;
90
+ -webkit-text-fill-color: transparent;
91
+ font-size: 3.5rem !important;
92
+ font-weight: 900;
93
+ text-align: center;
94
+ margin-bottom: 5px;
95
+ }
96
+
97
+ #subtitle_text h3 {
98
+ text-align: center;
99
+ color: #94a3b8;
100
+ font-size: 1.1rem;
101
+ margin-bottom: 30px;
102
+ }
103
+
104
+ .primary-btn {
105
+ background: linear-gradient(135deg, #fbbf24, #b45309) !important;
106
+ color: #000 !important;
107
+ border: none !important;
108
+ font-size: 1.2rem !important;
109
+ font-weight: 800 !important;
110
+ padding: 16px !important;
111
+ box-shadow: 0 10px 15px -3px rgba(245, 158, 11, 0.3);
112
+ }
113
+ """
114
+
115
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as app:
116
+ with gr.Column(elem_id="main_card"):
117
+ gr.Markdown("# TS VECTOR", elem_id="logo_text")
118
+ gr.Markdown("### IMAGE TO CNC VECTOR CONVERTER", elem_id="subtitle_text")
119
+
120
+ with gr.Row():
121
+ # Left: Inputs
122
+ with gr.Column(scale=1):
123
+ inp_img = gr.Image(type="pil", label="Upload Jali Design (Jpg/Png)")
124
+
125
+ with gr.Group():
126
+ gr.Markdown("### ⚙️ Trace Settings")
127
+ thresh = gr.Slider(0, 255, value=128, label="Detail Threshold (Darkness)")
128
+ smooth = gr.Slider(0.0, 1.33, value=1.0, label="Curve Smoothness (Low=Sharp, High=Round)")
129
+
130
+ btn = gr.Button("🔥 CONVERT TO SVG", variant="primary", elem_classes=["primary-btn"])
131
+
132
+ # Right: Outputs
133
+ with gr.Column(scale=1):
134
+ # Preview of what AI saw
135
+ preview_img = gr.Image(label="AI Vision (B&W Preview)", interactive=False)
136
+ # Download File
137
+ out_file = gr.File(label="Download Vector File (.SVG)")
138
+ gr.Markdown("*Note: Download SVG and open in CorelDraw/Illustrator.*")
139
+
140
+ btn.click(convert_to_vector, inputs=[inp_img, thresh, smooth], outputs=[out_file, preview_img])
141
+
142
+ app.launch()