\": {\\n \"message\": \"Inpaint image must match the requested width and height\...
🚨 错误信息
"HTTP错误: 400 - {'message': '{\\n \"error\": {\\n \"message\": \"Inpaint image must match the requested width and height\",\\n \"type\": \"invalid_request_error\",\\n \"param\": null,\\n \"code\": null\\n }\\n}', 'data': {'data': {'code': 'fail_to_fetch_task', 'message': '{\\n \"error\": {\\n \"message\": \"Inpaint image must match the requested width and height\",\\n \"type\": \"invalid_request_error\",\\n \"param\": null,\\n \"code\": null\\n }\\n}', 'type': 'task_error'}, 'message': '{\\n \"error\": {\\n \"message\": \"Inpaint image must match the requested width and height\",\\n \"type\": \"invalid_request_error\",\\n \"param\": null,\\n \"code\": null\\n }\\n}'}}"
🤖 AI解决方案
错误翻译
原文:"Inpaint image must match the requested width and height"
译文:"修复图像的尺寸必须与请求的宽度和高度一致"
原因分析
这是一个 400 Bad Request 错误。根本原因是您在调用图片修复(Inpainting)API时,提交的遮罩图像(mask image)或原图的尺寸与API请求参数中指定的
width 和 height 不匹配。错误明确指出API期望接收的图片尺寸必须与您在请求中声明的 width 和 height 参数值完全相同。
解决方案
您需要确保传递给API的图像数据(无论是通过URL还是Base64编码)的尺寸与请求参数一致。
Python (PIL示例)
python
from PIL import Image
import io假设您有一个名为 ‘image_to_inpaint’ 的图片变量(字节流或PIL Image对象)
假设您的目标尺寸是请求的宽度和高度
requested_width = 1024
requested_height = 768如果是字节流
image = Image.open(io.BytesIO(image_to_inpaint))
如果是PIL Image对象,直接使用
image = image_to_inpaint
检查并调整尺寸
if image.size != (requested_width, requested_height):
print(f"调整图片尺寸:从 {image.size} 到 ({requested_width}, {requested_height})")
# 使用高质量的缩放下采样
image = image.resize((requested_width, requested_height), Image.Resampling.LANCZOS)将处理后的图片转换为字节流,以便发送
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()现在,使用 img_byte_arr 作为API调用的图像数据
确保遮罩图片也经过相同的处理流程!
️ 预防措施
width 和 height 参数,从而避免尺寸不匹配问题。这通常是最可靠的方法。