gdTharusha commited on
Commit
886e2c5
1 Parent(s): 8a2db3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -13
app.py CHANGED
@@ -72,21 +72,27 @@ def preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail
72
 
73
  return image
74
 
 
 
 
 
 
 
 
 
 
 
 
75
  def convert_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization,
76
  color_mode, hierarchical, mode, filter_speckle, color_precision, layer_difference,
77
  corner_threshold, length_threshold, max_iterations, splice_threshold, path_precision,
78
- enhance_with_ai, remove_bg, upscale_factor):
79
  """Convert an image to SVG using vtracer with customizable and advanced parameters."""
80
 
81
  try:
82
  # Preprocess the image with additional detail level settings
83
  image = preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai, remove_bg)
84
 
85
- # Upscale the image if needed
86
- if upscale_factor > 1:
87
- new_size = (int(image.width * upscale_factor), int(image.height * upscale_factor))
88
- image = image.resize(new_size, Image.LANCZOS)
89
-
90
  # Convert Gradio image to bytes for vtracer compatibility
91
  img_byte_array = io.BytesIO()
92
  image.save(img_byte_array, format='PNG')
@@ -130,12 +136,14 @@ with iface:
130
 
131
  with gr.Row():
132
  image_input = gr.Image(type="pil", label="Upload Image")
 
 
 
133
  blur_radius_input = gr.Slider(minimum=0, maximum=10, value=0, step=0.1, label="Blur Radius (for smoothing)")
134
  sharpen_radius_input = gr.Slider(minimum=0, maximum=5, value=0, step=0.1, label="Sharpen Radius")
135
  noise_reduction_input = gr.Slider(minimum=0, maximum=30, value=0, step=1, label="Noise Reduction")
136
  enhance_with_ai_input = gr.Checkbox(label="AI Edge Enhance", value=False)
137
  remove_bg_input = gr.Checkbox(label="Remove Background", value=False)
138
- upscale_factor_input = gr.Slider(minimum=1, maximum=4, value=1, step=0.1, label="Upscale Factor (1 = No Upscaling)")
139
 
140
  with gr.Row():
141
  detail_level_input = gr.Slider(minimum=0, maximum=10, value=5, step=1, label="Detail Level")
@@ -161,20 +169,24 @@ with iface:
161
  splice_threshold_input = gr.Slider(minimum=1, maximum=100, value=45, step=1, label="Splice Threshold")
162
  path_precision_input = gr.Slider(minimum=1, maximum=100, value=8, step=1, label="Path Precision")
163
 
 
 
 
 
 
164
  convert_button = gr.Button("Convert Image to SVG")
165
  svg_output = gr.HTML(label="SVG Output")
166
  download_output = gr.File(label="Download SVG")
167
 
168
  convert_button.click(
169
- fn=convert_image,
170
  inputs=[
171
- image_input, blur_radius_input, sharpen_radius_input, noise_reduction_input, detail_level_input, edge_method_input, color_quantization_input,
172
- color_mode_input, hierarchical_input, mode_input, filter_speckle_input, color_precision_input,
173
- layer_difference_input, corner_threshold_input, length_threshold_input, max_iterations_input,
174
- splice_threshold_input, path_precision_input, enhance_with_ai_input, remove_bg_input, upscale_factor_input
175
  ],
176
  outputs=[svg_output, download_output]
177
  )
178
 
179
-
180
  iface.launch()
 
72
 
73
  return image
74
 
75
+ def upscale_image(image, upscale_factor):
76
+ """Upscale the image before further processing."""
77
+ try:
78
+ if upscale_factor > 1:
79
+ new_size = (int(image.width * upscale_factor), int(image.height * upscale_factor))
80
+ image = image.resize(new_size, Image.LANCZOS)
81
+ return image
82
+ except Exception as e:
83
+ print(f"Error during upscaling: {e}")
84
+ raise
85
+
86
  def convert_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization,
87
  color_mode, hierarchical, mode, filter_speckle, color_precision, layer_difference,
88
  corner_threshold, length_threshold, max_iterations, splice_threshold, path_precision,
89
+ enhance_with_ai, remove_bg):
90
  """Convert an image to SVG using vtracer with customizable and advanced parameters."""
91
 
92
  try:
93
  # Preprocess the image with additional detail level settings
94
  image = preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai, remove_bg)
95
 
 
 
 
 
 
96
  # Convert Gradio image to bytes for vtracer compatibility
97
  img_byte_array = io.BytesIO()
98
  image.save(img_byte_array, format='PNG')
 
136
 
137
  with gr.Row():
138
  image_input = gr.Image(type="pil", label="Upload Image")
139
+ upscale_factor_input = gr.Slider(minimum=1, maximum=4, value=1, step=0.1, label="Upscale Factor (1 = No Upscaling)")
140
+
141
+ with gr.Row():
142
  blur_radius_input = gr.Slider(minimum=0, maximum=10, value=0, step=0.1, label="Blur Radius (for smoothing)")
143
  sharpen_radius_input = gr.Slider(minimum=0, maximum=5, value=0, step=0.1, label="Sharpen Radius")
144
  noise_reduction_input = gr.Slider(minimum=0, maximum=30, value=0, step=1, label="Noise Reduction")
145
  enhance_with_ai_input = gr.Checkbox(label="AI Edge Enhance", value=False)
146
  remove_bg_input = gr.Checkbox(label="Remove Background", value=False)
 
147
 
148
  with gr.Row():
149
  detail_level_input = gr.Slider(minimum=0, maximum=10, value=5, step=1, label="Detail Level")
 
169
  splice_threshold_input = gr.Slider(minimum=1, maximum=100, value=45, step=1, label="Splice Threshold")
170
  path_precision_input = gr.Slider(minimum=1, maximum=100, value=8, step=1, label="Path Precision")
171
 
172
+ def process_and_convert(image, upscale_factor, *args):
173
+ """Handles upscaling and then calls the convert_image function."""
174
+ image = upscale_image(image, upscale_factor)
175
+ return convert_image(image, *args)
176
+
177
  convert_button = gr.Button("Convert Image to SVG")
178
  svg_output = gr.HTML(label="SVG Output")
179
  download_output = gr.File(label="Download SVG")
180
 
181
  convert_button.click(
182
+ fn=process_and_convert,
183
  inputs=[
184
+ image_input, upscale_factor_input, blur_radius_input, sharpen_radius_input, noise_reduction_input, detail_level_input, edge_method_input, color_quantization_input,
185
+ color_mode_input, hierarchical_input, mode_input, filter_speckle_input, color_precision_input, layer_difference_input,
186
+ corner_threshold_input, length_threshold_input, max_iterations_input, splice_threshold_input, path_precision_input,
187
+ enhance_with_ai_input, remove_bg_input
188
  ],
189
  outputs=[svg_output, download_output]
190
  )
191
 
 
192
  iface.launch()