Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,25 +27,36 @@ def upload_to_aws(local_file, s3_file):
|
|
| 27 |
print("Credentials not available")
|
| 28 |
return None
|
| 29 |
|
| 30 |
-
def convert_image(
|
| 31 |
color_precision, layer_difference, corner_threshold,
|
| 32 |
length_threshold, max_iterations, splice_threshold, path_precision):
|
| 33 |
-
"""Converts an image from a URL to SVG using vtracer with customizable parameters."""
|
| 34 |
-
|
| 35 |
-
# Debugging: Print the received image_url and its type
|
| 36 |
-
print(f"Received image_url: {image_url}, type: {type(image_url)}")
|
| 37 |
-
|
| 38 |
-
if not isinstance(image_url, str):
|
| 39 |
-
return gr.HTML(f'<p style="color: red;">Error: Expected a string URL, got {type(image_url)}</p>'), None, None
|
| 40 |
|
| 41 |
try:
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Load the image using PIL
|
| 48 |
image = Image.open(io.BytesIO(image_data))
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Convert the image to bytes for vtracer compatibility
|
| 51 |
img_byte_array = io.BytesIO()
|
|
@@ -53,6 +64,14 @@ def convert_image(image_url, color_mode, hierarchical, mode, filter_speckle,
|
|
| 53 |
image.save(img_byte_array, format=img_format)
|
| 54 |
img_bytes = img_byte_array.getvalue()
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# Perform the conversion
|
| 57 |
svg_str = vtracer.convert_raw_image_to_svg(
|
| 58 |
img_bytes,
|
|
@@ -69,6 +88,7 @@ def convert_image(image_url, color_mode, hierarchical, mode, filter_speckle,
|
|
| 69 |
splice_threshold=int(splice_threshold),
|
| 70 |
path_precision=int(path_precision)
|
| 71 |
)
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
return gr.HTML(f'<p style="color: red;">Error during image processing or SVG conversion: {str(e)}</p>'), None, None
|
| 74 |
|
|
@@ -89,7 +109,7 @@ def convert_image(image_url, color_mode, hierarchical, mode, filter_speckle,
|
|
| 89 |
|
| 90 |
# Return the SVG wrapped in HTML and the file URL
|
| 91 |
return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'), file_url, svg_str
|
| 92 |
-
|
| 93 |
|
| 94 |
# Gradio interface
|
| 95 |
iface = gr.Blocks()
|
|
|
|
| 27 |
print("Credentials not available")
|
| 28 |
return None
|
| 29 |
|
| 30 |
+
def convert_image(image_input, color_mode, hierarchical, mode, filter_speckle,
|
| 31 |
color_precision, layer_difference, corner_threshold,
|
| 32 |
length_threshold, max_iterations, splice_threshold, path_precision):
|
| 33 |
+
"""Converts an image from a URL or file path to SVG using vtracer with customizable parameters."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
try:
|
| 36 |
+
# Debugging: Print the received input and its type
|
| 37 |
+
print(f"Received image_input: {image_input}, type: {type(image_input)}")
|
| 38 |
+
|
| 39 |
+
# Handle both URL strings and Gradio FileData objects
|
| 40 |
+
if isinstance(image_input, dict) and 'path' in image_input:
|
| 41 |
+
# If the input is a Gradio FileData object, use the file path
|
| 42 |
+
image_path = image_input['path']
|
| 43 |
+
with open(image_path, 'rb') as f:
|
| 44 |
+
image_data = f.read()
|
| 45 |
+
elif isinstance(image_input, str):
|
| 46 |
+
# If the input is a URL, fetch the image
|
| 47 |
+
if image_input.startswith(('http://', 'https://')):
|
| 48 |
+
response = requests.get(image_input)
|
| 49 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 50 |
+
image_data = response.content
|
| 51 |
+
else:
|
| 52 |
+
return gr.HTML(f'<p style="color: red;">Error: Invalid URL format. Expected a valid HTTP/HTTPS URL.</p>'), None, None
|
| 53 |
+
else:
|
| 54 |
+
return gr.HTML(f'<p style="color: red;">Error: Invalid input type. Expected a URL or file path, got {type(image_input)}</p>'), None, None
|
| 55 |
|
| 56 |
# Load the image using PIL
|
| 57 |
image = Image.open(io.BytesIO(image_data))
|
| 58 |
+
print(f"Image format: {image.format}, Size: {image.size}, Mode: {image.mode}")
|
| 59 |
+
image.show() # Display the image for debugging
|
| 60 |
|
| 61 |
# Convert the image to bytes for vtracer compatibility
|
| 62 |
img_byte_array = io.BytesIO()
|
|
|
|
| 64 |
image.save(img_byte_array, format=img_format)
|
| 65 |
img_bytes = img_byte_array.getvalue()
|
| 66 |
|
| 67 |
+
# Debugging: Save the input image to a temporary file
|
| 68 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_img_file:
|
| 69 |
+
image.save(temp_img_file, format='PNG')
|
| 70 |
+
print(f"Saved input image to: {temp_img_file.name}")
|
| 71 |
+
|
| 72 |
+
# Debugging: Print vtracer parameters
|
| 73 |
+
print(f"vtracer parameters: img_format={img_format}, colormode={color_mode}, hierarchical={hierarchical}, mode={mode}, filter_speckle={filter_speckle}, color_precision={color_precision}, layer_difference={layer_difference}, corner_threshold={corner_threshold}, length_threshold={length_threshold}, max_iterations={max_iterations}, splice_threshold={splice_threshold}, path_precision={path_precision}")
|
| 74 |
+
|
| 75 |
# Perform the conversion
|
| 76 |
svg_str = vtracer.convert_raw_image_to_svg(
|
| 77 |
img_bytes,
|
|
|
|
| 88 |
splice_threshold=int(splice_threshold),
|
| 89 |
path_precision=int(path_precision)
|
| 90 |
)
|
| 91 |
+
print(f"Generated SVG: {svg_str}") # Debugging: Print the generated SVG
|
| 92 |
except Exception as e:
|
| 93 |
return gr.HTML(f'<p style="color: red;">Error during image processing or SVG conversion: {str(e)}</p>'), None, None
|
| 94 |
|
|
|
|
| 109 |
|
| 110 |
# Return the SVG wrapped in HTML and the file URL
|
| 111 |
return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'), file_url, svg_str
|
| 112 |
+
|
| 113 |
|
| 114 |
# Gradio interface
|
| 115 |
iface = gr.Blocks()
|