\": {\\n \"message\": \"Inpaint image must match the requested width and height\...

2025年12月27日 21:08 状态: processing

🚨 错误信息

"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请求参数中指定的 widthheight 不匹配。

错误明确指出API期望接收的图片尺寸必须与您在请求中声明的 widthheight 参数值完全相同。

解决方案


您需要确保传递给API的图像数据(无论是通过URL还是Base64编码)的尺寸与请求参数一致。

  • 检查并统一尺寸:在发送请求前,使用图像处理库(如PIL/Pillow for Python)检查并调整图片和遮罩的尺寸。

  • 修正请求参数或图片:要么将请求参数调整为图片的实际尺寸,要么将图片缩放/裁剪到请求的尺寸。
  • 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调用的图像数据


    确保遮罩图片也经过相同的处理流程!


    ️ 预防措施


  • 预处理标准化:在开发中,建立一个图片预处理函数,强制将所有输入图片(原图和遮罩)在发送API请求前,统一调整到目标尺寸。

  • 动态参数匹配:考虑设计您的程序逻辑,使其能先读取图片的实际尺寸,然后将这些尺寸值动态地设置为API请求的 widthheight 参数,从而避免尺寸不匹配问题。这通常是最可靠的方法。