Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Bug]: {'error': 'ValueError', 'detail': '', 'body': '', 'errors': 'height and width must be > 0'}
### Is there an existing issue for this?

- [X] I have searched the existing issues and checked the recent builds/commits

### What happened?

When I call the img2img API, sometimes this error is reported, but if I don't use graffiti without mask parameters, there is no problem



### Steps to reproduce the problem

call img2img api with mak param

### What should have happened?

`API error: POST: http://127.0.0.1:7161/sdapi/v1/img2img {'error': 'ValueError', 'detail': '', 'body': '', 'errors': 'height and width must be > 0'}
Traceback (most recent call last):
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/streams/memory.py", line 94, in receive
    return self.receive_nowait()
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/streams/memory.py", line 89, in receive_nowait
    raise WouldBlock
anyio.WouldBlock

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 78, in call_next
    message = await recv_stream.receive()
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/streams/memory.py", line 114, in receive
    raise EndOfStream
anyio.EndOfStream

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/autodl-tmp/stable-diffusion-webui/modules/api/api.py", line 152, in exception_handling
    return await call_next(request)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 84, in call_next
    raise app_exc
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 70, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 108, in __call__
    response = await self.dispatch_func(request, call_next)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/api/api.py", line 117, in log_and_time
    res: Response = await call_next(req)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 84, in call_next
    raise app_exc
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 70, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/cors.py", line 84, in __call__
    await self.app(scope, receive, send)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 24, in __call__
    await responder(scope, receive, send)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 44, in __call__
    await self.app(scope, receive, self.send_with_gzip)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app
    raw_response = await run_endpoint_function(
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/fastapi/routing.py", line 165, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/api/api.py", line 392, in img2imgapi
    processed = process_images(p)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/processing.py", line 610, in process_images
    res = process_images_inner(p)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/processing.py", line 670, in process_images_inner
    p.init(p.all_prompts, p.all_seeds, p.all_subseeds)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/processing.py", line 1161, in init
    image_mask = images.resize_image(2, mask, self.width, self.height)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/images.py", line 305, in resize_image
    res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/PIL/Image.py", line 2193, in resize
    return self._new(self.im.resize(size, resample, box))
ValueError: height and width must be > 0`

### Commit where the problem happens

master

### What Python version are you running on ?

Python 3.10.x

### What platforms do you use to access the UI ?

Linux

### What device are you running WebUI on?

Other GPUs

### What browsers do you use to access the UI ?

Google Chrome

### Command Line Arguments

```Shell
*
```


### List of extensions

*

### Console logs

```Shell
`API error: POST: http://127.0.0.1:7161/sdapi/v1/img2img {'error': 'ValueError', 'detail': '', 'body': '', 'errors': 'height and width must be > 0'}
Traceback (most recent call last):
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/streams/memory.py", line 94, in receive
    return self.receive_nowait()
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/streams/memory.py", line 89, in receive_nowait
    raise WouldBlock
anyio.WouldBlock

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 78, in call_next
    message = await recv_stream.receive()
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/streams/memory.py", line 114, in receive
    raise EndOfStream
anyio.EndOfStream

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/autodl-tmp/stable-diffusion-webui/modules/api/api.py", line 152, in exception_handling
    return await call_next(request)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 84, in call_next
    raise app_exc
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 70, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 108, in __call__
    response = await self.dispatch_func(request, call_next)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/api/api.py", line 117, in log_and_time
    res: Response = await call_next(req)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 84, in call_next
    raise app_exc
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/base.py", line 70, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/cors.py", line 84, in __call__
    await self.app(scope, receive, send)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 24, in __call__
    await responder(scope, receive, send)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 44, in __call__
    await self.app(scope, receive, self.send_with_gzip)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app
    raw_response = await run_endpoint_function(
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/fastapi/routing.py", line 165, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/api/api.py", line 392, in img2imgapi
    processed = process_images(p)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/processing.py", line 610, in process_images
    res = process_images_inner(p)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/processing.py", line 670, in process_images_inner
    p.init(p.all_prompts, p.all_seeds, p.all_subseeds)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/processing.py", line 1161, in init
    image_mask = images.resize_image(2, mask, self.width, self.height)
  File "/root/autodl-tmp/stable-diffusion-webui/modules/images.py", line 305, in resize_image
    res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
  File "/root/miniconda3/envs/xl_env/lib/python3.10/site-packages/PIL/Image.py", line 2193, in resize
    return self._new(self.im.resize(size, resample, box))
ValueError: height and width must be > 0`
```


### Additional information

nothing


Discussion:
same error
same here

Resolving patch:
diff --git a/modules/images.py b/modules/images.py
--- a/modules/images.py
+++ b/modules/images.py
@@ -302,12 +302,14 @@ def resize(im, w, h):
 
         if ratio < src_ratio:
             fill_height = height // 2 - src_h // 2
-            res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
-            res.paste(resized.resize((width, fill_height), box=(0, resized.height, width, resized.height)), box=(0, fill_height + src_h))
+            if fill_height > 0:
+                res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
+                res.paste(resized.resize((width, fill_height), box=(0, resized.height, width, resized.height)), box=(0, fill_height + src_h))
         elif ratio > src_ratio:
             fill_width = width // 2 - src_w // 2
-            res.paste(resized.resize((fill_width, height), box=(0, 0, 0, height)), box=(0, 0))
-            res.paste(resized.resize((fill_width, height), box=(resized.width, 0, resized.width, height)), box=(fill_width + src_w, 0))
+            if fill_width > 0:
+                res.paste(resized.resize((fill_width, height), box=(0, 0, 0, height)), box=(0, 0))
+                res.paste(resized.resize((fill_width, height), box=(resized.width, 0, resized.width, height)), box=(fill_width + src_w, 0))
 
     return res
 


Test patch: