Spaces:
Running
Running
ArrcttacsrjksX
commited on
Upload app(41).py
Browse files- Backup/app(41).py +75 -0
Backup/app(41).py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import tempfile
|
5 |
+
|
6 |
+
def convert_image_to_dxf(image_file, output_folder=None):
|
7 |
+
try:
|
8 |
+
# Validate input image
|
9 |
+
if image_file is None:
|
10 |
+
return "No image provided", None, None
|
11 |
+
|
12 |
+
# Define output folder, using a temp directory if none is specified
|
13 |
+
if not output_folder:
|
14 |
+
output_folder = tempfile.mkdtemp()
|
15 |
+
|
16 |
+
# Prepare paths for DXF and debug output
|
17 |
+
image_path = image_file
|
18 |
+
output_path = os.path.join(output_folder, os.path.splitext(os.path.basename(image_path))[0] + ".dxf")
|
19 |
+
debug_output_path = os.path.join(output_folder, os.path.splitext(os.path.basename(image_path))[0] + "_debug.png")
|
20 |
+
|
21 |
+
# Execute conversion command
|
22 |
+
result = subprocess.run(
|
23 |
+
["./SimpleImageToDxfHavePass", "--use-lines", f"--imagePath={image_path}", f"--outputPath={output_path}", f"--debug-output={debug_output_path}"],
|
24 |
+
check=True,
|
25 |
+
capture_output=True
|
26 |
+
)
|
27 |
+
|
28 |
+
# Check if conversion was successful
|
29 |
+
if not os.path.exists(output_path):
|
30 |
+
return "Conversion failed: DXF file was not created.", None, None
|
31 |
+
|
32 |
+
return f"DXF file saved to: {output_path}", debug_output_path, output_path
|
33 |
+
|
34 |
+
except subprocess.CalledProcessError as e:
|
35 |
+
error_msg = f"Error converting image to DXF: {e.stderr.decode('utf-8') if e.stderr else e}"
|
36 |
+
return error_msg, None, None
|
37 |
+
|
38 |
+
def main():
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
with gr.Tabs():
|
41 |
+
# Tab for conversion
|
42 |
+
with gr.TabItem("Image to DXF"):
|
43 |
+
gr.Markdown("# SimpleImageToDxfHavePass")
|
44 |
+
|
45 |
+
# Input row for image and optional output folder
|
46 |
+
with gr.Row():
|
47 |
+
image_input = gr.Image(type="filepath", label="Input Image (PNG/JPG)")
|
48 |
+
output_folder = gr.Textbox(label="Output Folder (optional)", placeholder="Leave blank to use a temporary folder")
|
49 |
+
|
50 |
+
# Outputs: Debug image and DXF file download link
|
51 |
+
with gr.Row():
|
52 |
+
debug_output = gr.Image(type="filepath", label="Debug Output Preview")
|
53 |
+
dxf_output = gr.File(label="DXF File Download")
|
54 |
+
|
55 |
+
# Conversion button with event binding
|
56 |
+
convert_btn = gr.Button("Convert to DXF")
|
57 |
+
convert_btn.click(
|
58 |
+
convert_image_to_dxf,
|
59 |
+
inputs=[image_input, output_folder],
|
60 |
+
outputs=[dxf_output, debug_output, dxf_output]
|
61 |
+
)
|
62 |
+
|
63 |
+
# About tab
|
64 |
+
with gr.TabItem("About"):
|
65 |
+
gr.Markdown("This Gradio app allows users to convert an image to a DXF file using the SimpleImageToDxfHavePass command-line tool.")
|
66 |
+
|
67 |
+
demo.launch()
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
try:
|
71 |
+
subprocess.run(['chmod', '+x', './SimpleImageToDxfHavePass'], check=True)
|
72 |
+
except subprocess.CalledProcessError as e:
|
73 |
+
print(f"Error setting permissions: {e}")
|
74 |
+
exit(1)
|
75 |
+
main()
|