Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,49 +26,67 @@ def upload_to_aws(local_file, s3_file):
|
|
| 26 |
print("Credentials not available")
|
| 27 |
return None
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
"""Converts an image to SVG using vtracer with customizable parameters."""
|
| 33 |
-
|
| 34 |
-
# Convert Gradio image to bytes for vtracer compatibility
|
| 35 |
-
img_byte_array = io.BytesIO()
|
| 36 |
-
image.save(img_byte_array, format='PNG')
|
| 37 |
-
img_bytes = img_byte_array.getvalue()
|
| 38 |
-
|
| 39 |
-
# Perform the conversion
|
| 40 |
-
svg_str = vtracer.convert_raw_image_to_svg(
|
| 41 |
-
img_bytes,
|
| 42 |
-
img_format='png',
|
| 43 |
-
colormode=color_mode.lower(),
|
| 44 |
-
hierarchical=hierarchical.lower(),
|
| 45 |
-
mode=mode.lower(),
|
| 46 |
-
filter_speckle=int(filter_speckle),
|
| 47 |
-
color_precision=int(color_precision),
|
| 48 |
-
layer_difference=int(layer_difference),
|
| 49 |
-
corner_threshold=int(corner_threshold),
|
| 50 |
-
length_threshold=float(length_threshold),
|
| 51 |
-
max_iterations=int(max_iterations),
|
| 52 |
-
splice_threshold=int(splice_threshold),
|
| 53 |
-
path_precision=int(path_precision)
|
| 54 |
-
)
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Save the SVG string to a temporary file
|
| 61 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.svg', mode='w', encoding='utf-8') as temp_file:
|
| 62 |
temp_file.write(svg_str)
|
| 63 |
temp_file_path = temp_file.name
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Return the SVG wrapped in HTML and the file URL
|
| 70 |
-
return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'), file_url, svg_str
|
|
|
|
| 71 |
|
|
|
|
| 72 |
# Gradio interface
|
| 73 |
iface = gr.Blocks()
|
| 74 |
|
|
@@ -76,7 +94,7 @@ with iface:
|
|
| 76 |
gr.Interface(
|
| 77 |
fn=convert_image,
|
| 78 |
inputs=[
|
| 79 |
-
gr.
|
| 80 |
gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode"),
|
| 81 |
gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical"),
|
| 82 |
gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"),
|
|
|
|
| 26 |
print("Credentials not available")
|
| 27 |
return None
|
| 28 |
|
| 29 |
+
def convert_image_from_url(image_url, color_mode, hierarchical, mode, filter_speckle,
|
| 30 |
+
color_precision, layer_difference, corner_threshold,
|
| 31 |
+
length_threshold, max_iterations, splice_threshold, path_precision):
|
| 32 |
+
"""Converts an image from a URL to SVG using vtracer with customizable parameters."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
try:
|
| 35 |
+
# Fetch the image from the URL
|
| 36 |
+
response = requests.get(image_url)
|
| 37 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 38 |
+
image_data = response.content
|
| 39 |
+
|
| 40 |
+
# Load the image using PIL
|
| 41 |
+
image = Image.open(io.BytesIO(image_data))
|
| 42 |
+
|
| 43 |
+
# Convert the image to bytes for vtracer compatibility
|
| 44 |
+
img_byte_array = io.BytesIO()
|
| 45 |
+
img_format = image.format if image.format else 'PNG' # Default to PNG for lossless quality
|
| 46 |
+
image.save(img_byte_array, format=img_format)
|
| 47 |
+
img_bytes = img_byte_array.getvalue()
|
| 48 |
+
|
| 49 |
+
# Perform the conversion
|
| 50 |
+
svg_str = vtracer.convert_raw_image_to_svg(
|
| 51 |
+
img_bytes,
|
| 52 |
+
img_format=img_format.lower(),
|
| 53 |
+
colormode=color_mode.lower(),
|
| 54 |
+
hierarchical=hierarchical.lower(),
|
| 55 |
+
mode=mode.lower(),
|
| 56 |
+
filter_speckle=int(filter_speckle),
|
| 57 |
+
color_precision=int(color_precision),
|
| 58 |
+
layer_difference=int(layer_difference),
|
| 59 |
+
corner_threshold=int(corner_threshold),
|
| 60 |
+
length_threshold=float(length_threshold),
|
| 61 |
+
max_iterations=int(max_iterations),
|
| 62 |
+
splice_threshold=int(splice_threshold),
|
| 63 |
+
path_precision=int(path_precision)
|
| 64 |
+
)
|
| 65 |
+
except Exception as e:
|
| 66 |
+
logging.error(f"Error during image processing or SVG conversion: {e}")
|
| 67 |
+
return gr.HTML(f'<p style="color: red;">Error during image processing or SVG conversion: {str(e)}</p>'), None, None
|
| 68 |
|
| 69 |
# Save the SVG string to a temporary file
|
| 70 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.svg', mode='w', encoding='utf-8') as temp_file:
|
| 71 |
temp_file.write(svg_str)
|
| 72 |
temp_file_path = temp_file.name
|
| 73 |
|
| 74 |
+
try:
|
| 75 |
+
# Upload the SVG file to AWS S3
|
| 76 |
+
s3_file_name = temp_file_path.split('/')[-1] # Use the temp file name as the S3 file name
|
| 77 |
+
file_url = upload_to_aws(temp_file_path, s3_file_name)
|
| 78 |
+
except Exception as e:
|
| 79 |
+
logging.error(f"Error uploading to AWS: {e}")
|
| 80 |
+
return gr.HTML(f'<p style="color: red;">Error uploading to AWS: {str(e)}</p>'), None, None
|
| 81 |
+
|
| 82 |
+
# Clean up the temporary file
|
| 83 |
+
os.unlink(temp_file_path)
|
| 84 |
|
| 85 |
# Return the SVG wrapped in HTML and the file URL
|
| 86 |
+
return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'), file_url, svg_str
|
| 87 |
+
|
| 88 |
|
| 89 |
+
|
| 90 |
# Gradio interface
|
| 91 |
iface = gr.Blocks()
|
| 92 |
|
|
|
|
| 94 |
gr.Interface(
|
| 95 |
fn=convert_image,
|
| 96 |
inputs=[
|
| 97 |
+
gr.Textbox(label="Image URL"),
|
| 98 |
gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode"),
|
| 99 |
gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical"),
|
| 100 |
gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"),
|