Execute img2img with the Stable Diffusion WebUI API, the image size changes unintentionally

58 Views Asked by At

I was writing a script to generate Controlnet Canny map images via API.
I noticed that the size of the image returned from the API is smaller than the original image size.

original image size: (719, 899)
responsed map image size: (712, 896)

However, since I set the original image size with payload's "width" and "height", I don't understand why this kind of result would occur.

I spent a few days on this issue without any good results.
Does anyone know the solution?

This sentence is a machine translation, so I apologize if it looks unnatural.

image = Image.open(image_path)

with io.BytesIO() as img_bytes:
    image.save(img_bytes, format='PNG')
    img_b64 = base64.b64encode(img_bytes.getvalue()).decode()

payload = {
    "steps": 1,
    "sampler_name": "Euler a",
    "denoising_strength": 0,
    "restore_faces": False,
    "resize_mode": 0,
    "init_images": [img_b64],
    "width": image.width,
    "height": image.height,
    "alwayson_scripts": {"ControlNet": {"args": [
        {
            "control_mode": 0,
            "enabled": True,
            "guidance_end": 1,
            "guidance_start": 0,
            "pixel_perfect": False,
            "processor_res": 512,
            "resize_mode": 1,
            "threshold_a": 100,
            "threshold_b": 135,
            "weight": 1,
            "module": "canny",
            "model": "control_canny-fp16 [e3fe7712]",
        }
    ]}}
}

response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload)
r = response.json()

map_image = Image.open(io.BytesIO(base64.b64decode(r['images'][1].split(",", 1)[0])))
root, ext = os.path.splitext(image_path)
basename = os.path.basename(root)
save_path = os.path.join(
    tools_dir, "canny_dst", basename+ext)
map_image.save(save_path)

environments:
I'm using Forge version.
Extensions other than the built-in ones are disabled.

"Platform": "Windows-10-10.0.19045-SP0",
"Python": "3.10.9",
"Version": "f0.0.17v1.8.0rc-latest-273-gb9705c58",
"Commit": "b9705c58f66c6fd2c4a0168b26c5cf1fa6c0dde3",
"Commandline": [
    "launch.py",
    "--port",
    "7861",
    "--always-gpu",
    "--api"
],

I also tried values of 1,2,3 for "resize_mode", but the result did not change.
Also, even if I removed the "alwayson_scripts" part of Controlnet, the result did not change.

0

There are 0 best solutions below