ArrcttacsrjksX commited on
Commit
e0f4bef
·
verified ·
1 Parent(s): 1859cd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -10
app.py CHANGED
@@ -1,31 +1,70 @@
1
  import gradio as gr
2
  import subprocess
3
  import os
 
4
 
5
- def convert_image_to_dxf(image_path, output_path, debug_output):
6
  try:
7
- subprocess.run(["./SimpleImageToDxfHavePass", "--use-lines", f"--imagePath={image_path}", f"--outputPath={output_path}", f"--debug-output={debug_output}"], check=True)
8
- return f"DXF file saved to: {output_path}", debug_output, output_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  except subprocess.CalledProcessError as e:
10
- return f"Error converting image to DXF: {e}", None, None
 
11
 
12
  def main():
13
  with gr.Blocks() as demo:
14
  with gr.Tabs():
 
15
  with gr.TabItem("Image to DXF"):
16
  gr.Markdown("# SimpleImageToDxfHavePass")
 
 
17
  with gr.Row():
18
- image_input = gr.Image(label="Input Image")
19
- dxf_output = gr.File(label="DXF Output")
 
 
20
  with gr.Row():
21
- debug_output = gr.Image(label="Debug Output")
22
- dxf_preview = gr.HTML(label="DXF Preview")
 
 
23
  convert_btn = gr.Button("Convert to DXF")
24
- convert_btn.click(convert_image_to_dxf, inputs=[image_input, dxf_output, debug_output], outputs=[dxf_output, debug_output, dxf_preview])
 
 
 
 
 
 
25
  with gr.TabItem("About"):
26
  gr.Markdown("This Gradio app allows users to convert an image to a DXF file using the SimpleImageToDxfHavePass command-line tool.")
27
 
28
  demo.launch()
29
 
30
  if __name__ == "__main__":
31
- main()
 
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
+ main()