Spaces:
Running
Running
import gradio as gr | |
import subprocess | |
import os | |
import tempfile | |
def convert_image_to_dxf(image_file, output_folder=None): | |
try: | |
# Validate input image | |
if image_file is None: | |
return "No image provided", None, None | |
# Define output folder, using a temp directory if none is specified | |
if not output_folder: | |
output_folder = tempfile.mkdtemp() | |
# Prepare paths for DXF and debug output | |
image_path = image_file | |
output_path = os.path.join(output_folder, os.path.splitext(os.path.basename(image_path))[0] + ".dxf") | |
debug_output_path = os.path.join(output_folder, os.path.splitext(os.path.basename(image_path))[0] + "_debug.png") | |
# Execute conversion command | |
result = subprocess.run( | |
["./SimpleImageToDxfHavePass", "--use-lines", f"--imagePath={image_path}", f"--outputPath={output_path}", f"--debug-output={debug_output_path}"], | |
check=True, | |
capture_output=True | |
) | |
# Check if conversion was successful | |
if not os.path.exists(output_path): | |
return "Conversion failed: DXF file was not created.", None, None | |
return f"DXF file saved to: {output_path}", debug_output_path, output_path | |
except subprocess.CalledProcessError as e: | |
error_msg = f"Error converting image to DXF: {e.stderr.decode('utf-8') if e.stderr else e}" | |
return error_msg, None, None | |
def main(): | |
with gr.Blocks() as demo: | |
with gr.Tabs(): | |
# Tab for conversion | |
with gr.TabItem("Image to DXF"): | |
gr.Markdown("# SimpleImageToDxfHavePass") | |
# Input row for image and optional output folder | |
with gr.Row(): | |
image_input = gr.Image(type="filepath", label="Input Image (PNG/JPG)") | |
output_folder = gr.Textbox(label="Output Folder (optional)", placeholder="Leave blank to use a temporary folder") | |
# Outputs: Debug image and DXF file download link | |
with gr.Row(): | |
debug_output = gr.Image(type="filepath", label="Debug Output Preview") | |
dxf_output = gr.File(label="DXF File Download") | |
# Conversion button with event binding | |
convert_btn = gr.Button("Convert to DXF") | |
convert_btn.click( | |
convert_image_to_dxf, | |
inputs=[image_input, output_folder], | |
outputs=[dxf_output, debug_output, dxf_output] | |
) | |
# About tab | |
with gr.TabItem("About"): | |
gr.Markdown("This Gradio app allows users to convert an image to a DXF file using the SimpleImageToDxfHavePass command-line tool.") | |
demo.launch() | |
if __name__ == "__main__": | |
try: | |
subprocess.run(['chmod', '+x', './SimpleImageToDxfHavePass'], check=True) | |
except subprocess.CalledProcessError as e: | |
print(f"Error setting permissions: {e}") | |
exit(1) | |
main() | |