codelion commited on
Commit
a1bff88
·
verified ·
1 Parent(s): 71de57f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -18
app.py CHANGED
@@ -31,18 +31,11 @@ def initial_render(svg_file):
31
  preview_image = Image.open(output_path)
32
  return preview_image, temp_svg_path
33
 
34
- def final_convert(svg_path, crop_box):
35
- """Convert SVG to PNG using user-specified crop box dimensions."""
36
- if svg_path is None or crop_box is None:
37
  return None, None
38
 
39
- # Extract crop coordinates from the crop_box dictionary
40
- # crop_box format: {'left': x1, 'top': y1, 'right': x2, 'bottom': y2}
41
- left = crop_box['left']
42
- top = crop_box['top']
43
- width = crop_box['right'] - left
44
- height = crop_box['bottom'] - top
45
-
46
  # Read the stored SVG content
47
  with open(svg_path, 'rb') as f:
48
  svg_content = f.read()
@@ -50,13 +43,29 @@ def final_convert(svg_path, crop_box):
50
  # Final output path
51
  output_path = "./final_output.png"
52
 
53
- # Convert with user-specified dimensions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  cairosvg.svg2png(
55
  bytestring=svg_content,
56
  write_to=output_path,
57
  output_width=width,
58
- output_height=height,
59
- # Optional: adjust scale or offset if needed based on original SVG coordinates
60
  )
61
 
62
  # Load the final PNG
@@ -64,22 +73,26 @@ def final_convert(svg_path, crop_box):
64
  return final_image, output_path
65
 
66
  with gr.Blocks() as bl:
67
- gr.Markdown("# SVG to PNG Converter with Custom Crop")
68
 
69
  with gr.Row():
70
  with gr.Column():
71
  svg_input = gr.File(label="Upload SVG File", file_types=[".svg"])
72
  preview_btn = gr.Button("Generate Preview")
 
 
 
 
73
 
74
  with gr.Column():
75
  preview_output = gr.Image(
76
  type='pil',
77
- label="Preview (Draw a box to crop)",
78
  interactive=True,
79
  tool="sketch",
80
  height=800
81
  )
82
- crop_btn = gr.Button("Convert with Crop")
83
  final_output = gr.Image(type='pil', label="Final PNG", height=800)
84
  download_btn = gr.File(label="Download Final PNG")
85
 
@@ -93,10 +106,10 @@ with gr.Blocks() as bl:
93
  outputs=[preview_output, svg_state]
94
  )
95
 
96
- # Step 2: Convert using crop box
97
  crop_btn.click(
98
  fn=final_convert,
99
- inputs=[svg_state, preview_output],
100
  outputs=[final_output, download_btn]
101
  )
102
 
 
31
  preview_image = Image.open(output_path)
32
  return preview_image, temp_svg_path
33
 
34
+ def final_convert(svg_path, crop_box, size_input):
35
+ """Convert SVG to PNG using either crop box or user-specified dimensions."""
36
+ if svg_path is None:
37
  return None, None
38
 
 
 
 
 
 
 
 
39
  # Read the stored SVG content
40
  with open(svg_path, 'rb') as f:
41
  svg_content = f.read()
 
43
  # Final output path
44
  output_path = "./final_output.png"
45
 
46
+ # Determine output dimensions
47
+ if size_input and size_input.strip(): # Check if size input is provided
48
+ try:
49
+ width, height = map(int, size_input.split('x'))
50
+ if width <= 0 or height <= 0:
51
+ raise ValueError("Width and height must be positive")
52
+ except ValueError:
53
+ return gr.Warning("Invalid size format. Use 'width x height' (e.g., '800x600')"), None
54
+ elif crop_box and 'left' in crop_box: # Use crop box if size input is empty
55
+ left = crop_box['left']
56
+ top = crop_box['top']
57
+ width = crop_box['right'] - left
58
+ height = crop_box['bottom'] - top
59
+ else:
60
+ # Fallback if neither is provided (shouldn't happen with UI flow)
61
+ width, height = 2000, 2000
62
+
63
+ # Convert SVG to PNG with determined dimensions
64
  cairosvg.svg2png(
65
  bytestring=svg_content,
66
  write_to=output_path,
67
  output_width=width,
68
+ output_height=height
 
69
  )
70
 
71
  # Load the final PNG
 
73
  return final_image, output_path
74
 
75
  with gr.Blocks() as bl:
76
+ gr.Markdown("# SVG to PNG Converter with Custom Crop or Size")
77
 
78
  with gr.Row():
79
  with gr.Column():
80
  svg_input = gr.File(label="Upload SVG File", file_types=[".svg"])
81
  preview_btn = gr.Button("Generate Preview")
82
+ size_input = gr.Textbox(
83
+ label="Output Size (width x height)",
84
+ placeholder="e.g., 800x600 (optional, overrides crop)"
85
+ )
86
 
87
  with gr.Column():
88
  preview_output = gr.Image(
89
  type='pil',
90
+ label="Preview (Draw a box to crop if no size specified)",
91
  interactive=True,
92
  tool="sketch",
93
  height=800
94
  )
95
+ crop_btn = gr.Button("Convert")
96
  final_output = gr.Image(type='pil', label="Final PNG", height=800)
97
  download_btn = gr.File(label="Download Final PNG")
98
 
 
106
  outputs=[preview_output, svg_state]
107
  )
108
 
109
+ # Step 2: Convert using crop box or specified size
110
  crop_btn.click(
111
  fn=final_convert,
112
+ inputs=[svg_state, preview_output, size_input],
113
  outputs=[final_output, download_btn]
114
  )
115