": "status_code=400, body={\"code\":305000002,\"message\":\"Function execution f...
🚨 错误信息
🤖 AI解决方案
代码报错分析
1. 错误翻译
原文:Invalid value for an element in the 'referenceImages' parameter. Each reference image must be specified in one of the following formats: a UUID v4 string of a previously uploaded or generated image, a data URI string, a base64-encoded image, or a publicly accessible URL. Supported formats are: PNG, JPG, and WEBP.
译文:'referenceImages'参数中的元素值无效。每个参考图像必须以以下格式之一指定:先前上传或生成的图像的UUID v4字符串、数据URI字符串、base64编码图像或可公开访问的URL。支持的格式为:PNG、JPG和WEBP。
2. 原因分析
这是API调用参数验证错误,具体问题出现在
referenceImages参数上:Merge_three_images_CN.py文件的handler函数第79行3. 解决方案
检查并修正
referenceImages参数的格式:python
正确示例 - 使用base64编码
import base64def read_image_to_base64(image_path):
with open(image_path, 'rb') as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
或者使用数据URI格式
def create_data_uri(image_path):
import mimetypes
mime_type, _ = mimetypes.guess_type(image_path)
with open(image_path, 'rb') as image_file:
base64_data = base64.b64encode(image_file.read()).decode('utf-8')
return f"data:{mime_type};base64,{base64_data}"在API调用中使用
reference_images = [
read_image_to_base64('image1.png'), # base64格式
create_data_uri('image2.jpg'), # 数据URI格式
"https://example.com/image3.webp" # 公开URL格式
]
4. ️ 预防措施
referenceImages数组中的每个元素格式是否正确python
import re
import urllib.parsedef validate_reference_image(image_input):
# 检查UUID格式
uuid_pattern = re.compile(r'^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$', re.I)
if uuid_pattern.match(image_input):
return True
# 检查数据URI格式
if image_input.startswith('data:image/'):
return True
# 检查base64编码(简单的格式检查)
if re.match(r'^[A-Za-z0-9+/]*={0,2}$', image_input) and len(image_input) > 100:
return True
# 检查URL格式
try:
result = urllib.parse.urlparse(image_input)
return all([result.scheme, result.netloc])
except:
return False
return False
---
请确保您的参考图像格式符合上述要求后重新调用API。