Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import io
|
|
| 6 |
import os
|
| 7 |
import logging
|
| 8 |
import base64
|
|
|
|
| 9 |
|
| 10 |
# Set up logging
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -80,30 +81,69 @@ def convert_image():
|
|
| 80 |
# Handle JSON payload
|
| 81 |
data = request.get_json()
|
| 82 |
if not data or 'file' not in data:
|
|
|
|
| 83 |
return jsonify({'error': 'No image data provided'}), 400
|
| 84 |
|
| 85 |
-
#
|
| 86 |
base64_string = data['file']
|
| 87 |
-
if base64_string
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
try:
|
| 90 |
-
image_data = base64.b64decode(
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# Get parameters (with defaults)
|
| 96 |
colormode = data.get('colormode', 'color')
|
| 97 |
hierarchical = data.get('hierarchical', 'stacked')
|
| 98 |
mode = data.get('mode', 'spline')
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
logger.info("Received request to /convert")
|
| 109 |
|
|
|
|
| 6 |
import os
|
| 7 |
import logging
|
| 8 |
import base64
|
| 9 |
+
import re
|
| 10 |
|
| 11 |
# Set up logging
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 81 |
# Handle JSON payload
|
| 82 |
data = request.get_json()
|
| 83 |
if not data or 'file' not in data:
|
| 84 |
+
logger.error("No image data provided in request")
|
| 85 |
return jsonify({'error': 'No image data provided'}), 400
|
| 86 |
|
| 87 |
+
# Extract base64 string
|
| 88 |
base64_string = data['file']
|
| 89 |
+
if not base64_string:
|
| 90 |
+
logger.error("Empty base64 string")
|
| 91 |
+
return jsonify({'error': 'Invalid base64 image data: empty string'}), 400
|
| 92 |
+
|
| 93 |
+
# Parse data URL
|
| 94 |
+
match = re.match(r'^data:image/(\w+);base64,(.+)$', base64_string)
|
| 95 |
+
if not match:
|
| 96 |
+
logger.error("Invalid data URL format")
|
| 97 |
+
return jsonify({'error': 'Invalid base64 image data: incorrect data URL format'}), 400
|
| 98 |
+
|
| 99 |
+
image_format, base64_data = match.groups()
|
| 100 |
+
if image_format not in ['jpeg', 'png']:
|
| 101 |
+
logger.error(f"Unsupported image format: {image_format}")
|
| 102 |
+
return jsonify({'error': f'Invalid base64 image data: unsupported format {image_format}'}), 400
|
| 103 |
+
|
| 104 |
+
# Validate base64 string
|
| 105 |
+
if not re.match(r'^[A-Za-z0-9+/=]+$', base64_data):
|
| 106 |
+
logger.error("Invalid base64 string characters")
|
| 107 |
+
return jsonify({'error': 'Invalid base64 image data: contains invalid characters'}), 400
|
| 108 |
+
|
| 109 |
+
# Decode base64
|
| 110 |
try:
|
| 111 |
+
image_data = base64.b64decode(base64_data, validate=True)
|
| 112 |
+
logger.info(f"Decoded base64 data, length: {len(image_data)}")
|
| 113 |
except Exception as e:
|
| 114 |
+
logger.error(f"Base64 decode failed: {str(e)}")
|
| 115 |
+
return jsonify({'error': f'Invalid base64 image data: {str(e)}'}), 400
|
| 116 |
+
|
| 117 |
+
# Validate image data
|
| 118 |
+
if len(image_data) == 0:
|
| 119 |
+
logger.error("Decoded image data is empty")
|
| 120 |
+
return jsonify({'error': 'Invalid base64 image data: empty after decoding'}), 400
|
| 121 |
+
|
| 122 |
+
# Process image
|
| 123 |
+
try:
|
| 124 |
+
image = Image.open(io.BytesIO(image_data))
|
| 125 |
+
if image_format == 'png':
|
| 126 |
+
image = image.convert('RGB') # Convert PNG to RGB for consistency
|
| 127 |
+
except Exception as e:
|
| 128 |
+
logger.error(f"Image processing failed: {str(e)}")
|
| 129 |
+
return jsonify({'error': f'Failed to process image: {str(e)}'}), 400
|
| 130 |
|
| 131 |
# Get parameters (with defaults)
|
| 132 |
colormode = data.get('colormode', 'color')
|
| 133 |
hierarchical = data.get('hierarchical', 'stacked')
|
| 134 |
mode = data.get('mode', 'spline')
|
| 135 |
+
try:
|
| 136 |
+
filter_speckle = int(data.get('filter_speckle', 4))
|
| 137 |
+
color_precision = int(data.get('color_precision', 6))
|
| 138 |
+
layer_difference = int(data.get('layer_difference', 16))
|
| 139 |
+
corner_threshold = int(data.get('corner_threshold', 60))
|
| 140 |
+
length_threshold = float(data.get('length_threshold', 4.0))
|
| 141 |
+
max_iterations = int(data.get('max_iterations', 10))
|
| 142 |
+
splice_threshold = int(data.get('splice_threshold', 45))
|
| 143 |
+
path_precision = int(data.get('path_precision', 3))
|
| 144 |
+
except (ValueError, TypeError) as e:
|
| 145 |
+
logger.error(f"Invalid parameter format: {str(e)}")
|
| 146 |
+
return jsonify({'error': f'Invalid parameter format: {str(e)}'}), 400
|
| 147 |
|
| 148 |
logger.info("Received request to /convert")
|
| 149 |
|