Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ CORS(app, resources={r"/convert-to-vector": {"origins": "*"}})
|
|
| 13 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 14 |
|
| 15 |
def convert_to_vector(image):
|
| 16 |
-
input_path = "temp_input.
|
| 17 |
output_svg_path = "temp_output.svg"
|
| 18 |
|
| 19 |
try:
|
|
@@ -65,15 +65,34 @@ def convert_to_vector_endpoint():
|
|
| 65 |
# Handle image data
|
| 66 |
if request.data:
|
| 67 |
app.logger.debug("Received raw image data")
|
| 68 |
-
image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
elif 'image' in request.files:
|
| 70 |
app.logger.debug("Received multipart image data")
|
| 71 |
file = request.files['image']
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
app.logger.error("No image data provided")
|
| 75 |
return jsonify({'error': 'No image data provided'}), 400
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Process image
|
| 78 |
app.logger.debug("Processing image")
|
| 79 |
svg_content = convert_to_vector(image)
|
|
|
|
| 13 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 14 |
|
| 15 |
def convert_to_vector(image):
|
| 16 |
+
input_path = "temp_input.png" # Use .png to support PNG inputs
|
| 17 |
output_svg_path = "temp_output.svg"
|
| 18 |
|
| 19 |
try:
|
|
|
|
| 65 |
# Handle image data
|
| 66 |
if request.data:
|
| 67 |
app.logger.debug("Received raw image data")
|
| 68 |
+
# Try to open the image without assuming format
|
| 69 |
+
try:
|
| 70 |
+
image = Image.open(io.BytesIO(request.data))
|
| 71 |
+
except Exception as e:
|
| 72 |
+
app.logger.error(f"Failed to open image: {str(e)}")
|
| 73 |
+
return jsonify({'error': f"Invalid image data: {str(e)}"}), 400
|
| 74 |
elif 'image' in request.files:
|
| 75 |
app.logger.debug("Received multipart image data")
|
| 76 |
file = request.files['image']
|
| 77 |
+
try:
|
| 78 |
+
image = Image.open(file)
|
| 79 |
+
except Exception as e:
|
| 80 |
+
app.logger.error(f"Failed to open image: {str(e)}")
|
| 81 |
+
return jsonify({'error': f"Invalid image data: {str(e)}"}), 400
|
| 82 |
else:
|
| 83 |
app.logger.error("No image data provided")
|
| 84 |
return jsonify({'error': 'No image data provided'}), 400
|
| 85 |
|
| 86 |
+
# Log image format and mode
|
| 87 |
+
app.logger.debug(f"Image format: {image.format}, mode: {image.mode}")
|
| 88 |
+
|
| 89 |
+
# Convert to RGB if needed (for vtracer compatibility)
|
| 90 |
+
if image.mode not in ['RGB', 'RGBA']:
|
| 91 |
+
image = image.convert('RGB')
|
| 92 |
+
elif image.mode == 'RGBA':
|
| 93 |
+
# Optionally convert RGBA to RGB to avoid alpha channel issues
|
| 94 |
+
image = image.convert('RGB')
|
| 95 |
+
|
| 96 |
# Process image
|
| 97 |
app.logger.debug("Processing image")
|
| 98 |
svg_content = convert_to_vector(image)
|