Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: When using Python to call the web UI API for mask image generation, a black bar appears at the bottom of the image. #16705

Open
1 of 6 tasks
hoyAng96 opened this issue Dec 4, 2024 · 0 comments
Labels
bug-report Report of a bug, yet to be confirmed

Comments

@hoyAng96
Copy link

hoyAng96 commented Dec 4, 2024

Checklist

  • The issue exists after disabling all extensions
  • The issue exists on a clean installation of webui
  • The issue is caused by an extension, but I believe it is caused by a bug in the webui
  • The issue exists in the current version of the webui
  • The issue has not been reported before recently
  • The issue has been reported before but has not been fixed yet

What happened?

I encountered this issue when using Python to call the API. I am using /sdapi/v1/img2img for image generation. Below is my calling code.

class SDWebUIRequest:
    def __init__(self, server_address="http://127.0.0.1:7860"):
        self.server_address = server_address
        self.api_endpoint = f"{self.server_address}/sdapi/v1/img2img"
    def img2img_with_canny_controlnet(self, prompt, original_image_path, mask_image_path, negative_prompt="", steps=20, seed=-1, cfg_scale=7.0, controlnet_model="control_canny-sd15", controlnet_weight=1.0):
        try:
            # 读取并编码原始图像
            with open(original_image_path, "rb") as img_file:
                original_image = base64.b64encode(img_file.read()).decode('utf-8')
            # 读取并编码蒙版图像
            with open(mask_image_path, "rb") as mask_file:
                mask_image = base64.b64encode(mask_file.read()).decode('utf-8')
            controlnet_config = {
                "enabled": True,
                "image": original_image,
                "weight": 1,
                "module": "canny",
                "model": "control_v11p_sd15_canny_fp16 [b18e0966]",
            }
            # 构建请求负载
            payload = {
                "prompt": prompt,
                "negative_prompt": negative_prompt,
                "sampler_name": "Euler a",
                "steps": 20,
                "seed": seed,
                "cfg_scale": cfg_scale,
                 "width": 1800,  # 根据需要调整
                "height": 1200, # 根据需要调整
                "init_images": [f"data:image/png;base64,{original_image}"],
                "mask": f"data:image/png;base64,{mask_image}",
                "resize_mode": 1,  # 调整全局 resize_mode 以适应需求
                "denoising_strength": 0.75,
                "alwayson_scripts": {
                    "controlnet": {
                        "args": [controlnet_config]
                    }
                }
            }
            headers = {
                'Content-Type': 'application/json'
            }
            print("发送到Stable Diffusion WebUI的请求负载:")
            # 发送POST请求
            response = requests.post(self.api_endpoint, headers=headers, data=json.dumps(payload))
            if response.status_code == 200:
                response_data = response.json()
                for i, image_data in enumerate(response_data.get("images", [])):
                    if ',' in image_data:
                        image_data = image_data.split(",",1)[1]
                    image = Image.open(io.BytesIO(base64.b64decode(image_data)))
                    save_path = os.path.join(TEMP_DIR, f"sd_output_{uuid.uuid4().hex}.png")
                    image.save(save_path)
                    print(f"生成的图像已保存到 {save_path}")
                    return save_path
            else:
                print(f"请求失败: {response.status_code} - {response.text}")
                return None
        except Exception as e:
            print(f"发送请求时出错: {e}")
            return None

The images generated through the web UI have no problems,

Uploading 00008-3395685328.png…

but when I use the same parameters to call the API, a black bar appears.

sd_output_c1fc196e45984eafaba1c8cbdd7c3cca

My image size is 1800x1200, and the mask is the same size,

output-1111111111111111-54-0

but there ends up being a curved edge at the bottom of the image. I'm not sure why this is happening. Additionally, when I set the mask for image generation, the width and height specified in the payload become ineffective. The generated image always ends up being 1800x1200, even if I set those two parameters to 512x512. I don't know what the issue is. When the size exceeds 512x512, the white border becomes very noticeable, but the image quality decreases significantly.

sd_output_ba3a63d25f5d43c7be87fb995c4193a6

If I set it to a larger size, such as the original size of 1800x1200, the image quality improves, but that curved edge becomes even more pronounced.

Steps to reproduce the problem

Using the code above to make the call results in this issue.

What should have happened?

Under normal circumstances, the generated image should be almost identical to the one produced by the web UI, without that white border!

What browsers do you use to access the UI ?

Microsoft Edge

Sysinfo

sysinfo-2024-12-04-01-10.json

Console logs

Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
Version: v1.10.1
Commit hash: 82a973c04367123ae98bd9abdf80d9eda9b910e2
Launching Web UI with arguments: --medvram-sdxl --theme dark --xformers --api --autolaunch --server-name 0.0.0.0 --skip-python-version-check
Tag Autocomplete: Could not locate model-keyword extension, Lora trigger word completion will be limited to those added through the extra networks menu.
[-] ADetailer initialized. version: 24.9.0, num models: 10
ControlNet preprocessor location: D:\software\server_tools\sd-webui-aki-v4.9.1\sd-webui-aki-v4.9.1\extensions\sd-webui-controlnet\annotator\downloads
2024-12-04 08:37:27,144 - ControlNet - INFO - ControlNet v1.1.455
sd-webui-prompt-all-in-one background API service started successfully.
Loading weights [7f96a1a9ca] from D:\software\server_tools\sd-webui-aki-v4.9.1\sd-webui-aki-v4.9.1\models\Stable-diffusion\sd1.5\anything-v5.safetensors
2024-12-04 08:37:32,317 - ControlNet - INFO - ControlNet UI callback registered.
Creating model from config: D:\software\server_tools\sd-webui-aki-v4.9.1\sd-webui-aki-v4.9.1\configs\v1-inference.yaml
Running on local URL:  http://0.0.0.0:7860

To create a public link, set `share=True` in `launch()`.
IIB Database file has been successfully backed up to the backup folder.
Startup time: 60.3s (prepare environment: 9.5s, import torch: 10.9s, import gradio: 6.6s, setup paths: 2.9s, initialize shared: 0.7s, other imports: 3.2s, setup gfpgan: 0.1s, list SD models: 0.3s, load scripts: 11.1s, initialize extra networks: 0.4s, scripts before_ui_callback: 0.1s, create ui: 3.4s, gradio launch: 8.6s, add APIs: 1.1s, app_started_callback: 3.0s).
Loading VAE weights specified in settings: D:\software\server_tools\sd-webui-aki-v4.9.1\sd-webui-aki-v4.9.1\models\VAE\sd1.5\animevae.pt
Applying attention optimization: xformers... done.
Model loaded in 36.3s (load weights from disk: 3.6s, load config: 0.3s, create model: 1.0s, apply weights to model: 21.9s, load VAE: 7.6s, load textual inversion embeddings: 0.2s, calculate empty prompt: 1.4s).
WARNING:root:Sampler Scheduler autocorrection: "Euler a" -> "Euler a", "None" -> "Automatic"
WARNING:root:Sampler Scheduler autocorrection: "Euler a" -> "Euler a", "None" -> "Automatic"
2024-12-04 08:53:53,160 - ControlNet - INFO - unit_separate = False, style_align = False
2024-12-04 08:53:53,398 - ControlNet - INFO - Loading model: control_v11p_sd15_canny_fp16 [b18e0966]
2024-12-04 08:53:53,969 - ControlNet - INFO - Loaded state_dict from [D:\software\server_tools\sd-webui-aki-v4.9.1\sd-webui-aki-v4.9.1\models\ControlNet\control_v11p_sd15_canny_fp16.safetensors]
2024-12-04 08:53:53,970 - ControlNet - INFO - controlnet_default_config
2024-12-04 08:54:04,569 - ControlNet - INFO - ControlNet model control_v11p_sd15_canny_fp16 [b18e0966](ControlModelType.ControlNet) loaded.
2024-12-04 08:54:04,989 - ControlNet - INFO - Using preprocessor: canny
2024-12-04 08:54:04,989 - ControlNet - INFO - preprocessor resolution = 512
2024-12-04 08:54:05,228 - ControlNet - INFO - ControlNet Hooked - Time = 12.071203470230103
WARNING:root:Sampler Scheduler autocorrection: "Euler a" -> "Euler a", "None" -> "Automatic"
2024-12-04 08:56:10,995 - ControlNet - INFO - unit_separate = False, style_align = False
2024-12-04 08:56:10,995 - ControlNet - INFO - Loading model from cache: control_v11p_sd15_canny_fp16 [b18e0966]
2024-12-04 08:56:11,383 - ControlNet - INFO - Using preprocessor: canny
2024-12-04 08:56:11,383 - ControlNet - INFO - preprocessor resolution = 512
2024-12-04 08:56:11,585 - ControlNet - INFO - ControlNet Hooked - Time = 0.5922081470489502

Additional information

No response

@hoyAng96 hoyAng96 added the bug-report Report of a bug, yet to be confirmed label Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-report Report of a bug, yet to be confirmed
Projects
None yet
Development

No branches or pull requests

1 participant