Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
img2img api   ValueError: Coordinate 'right' is less than 'left'
### Is there an existing issue for this?

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

### What happened?

if i use mask  and  set inpaint_full_res =true

pyton will tip ValueError: Coordinate 'right' is less than 'left'
![image](https://github.com/AUTOMATIC1111/stable-diffusion-webui/assets/3301385/89cc9654-ed2f-4c75-b61a-3249608bcfcf)


### Steps to reproduce the problem

1. Go to .... 
2. Press ....
3. ...


### What should have happened?

i hope it can working

### Commit where the problem happens

SHA-1: 5ab7f213bec2f816f9c5644becb32eb72c8ffb89

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

Windows

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

Google Chrome

### Command Line Arguments

```Shell
--api --listen
```


### List of extensions

no

### Console logs

```Shell
no
```


### Additional information

_No response_


Discussion:
This happens when you're inpainting but there's no mask. I noticed this as well, some bug remove the mask randomly, not sure how to reproduce this.
met the problem here, any solution? 
Gen picture and mask, have similar exception
<details>
  <summary>Picture and mask</summary>
 both are 512*512
 
![mask](https://github.com/AUTOMATIC1111/stable-diffusion-webui/assets/33497292/7ac7c55c-c9ad-4b40-b7df-8ef39c8b1539)
![image](https://github.com/AUTOMATIC1111/stable-diffusion-webui/assets/33497292/e6690e3e-9dae-4dac-9394-0aaf6753cda5)
</details>
I have the same exception.
> 

Draw on the image mate. :)
> > 
> 
> Draw on the image mate. :)

But how set, that it is an inpant, not just img2img?
> 

Like this. Note the size should be for your mask, so maybe double if you want it higher resolution - but not for the entire image, when you use inpaint.
![3 - LARGE image still paints only inside mask](https://github.com/AUTOMATIC1111/stable-diffusion-webui/assets/3269738/0a8549b0-e341-43ea-a993-cfc2c33fb9ec)

@andupotorac here we discuss an api, how to create curl request for inpanting draw?
> @andupotorac here we discuss an api, how to create curl request for inpanting draw?

Oh, didn't notice that. Can't help with the API.
I had this error, many times.

Today I'm using inpaint upload. Based on the testimony above I replaced transparency in my mask file with white, and the error was gone, but it's attempting to inpaint the white area. Disjointedly.


![problem0](https://github.com/AUTOMATIC1111/stable-diffusion-webui/assets/25666053/7ce46689-71ef-4437-bfb7-6195b7fe2052)

![problem1](https://github.com/AUTOMATIC1111/stable-diffusion-webui/assets/25666053/793cd688-5dcb-4d54-9c51-9d09b1d36c5a)


The problem here is the webui expects the mask input to be a binary mask (RGB, only white and black), not an alpha mask. That being said some conversion should ideally happen automatically to correct this.

Edit: Actually looks like some upstream breaking change with Gradio broke this. Will have a fix shortly.

Resolving patch:
diff --git a/modules/img2img.py b/modules/img2img.py
--- a/modules/img2img.py
+++ b/modules/img2img.py
@@ -129,7 +129,7 @@ def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_s
         mask = None
     elif mode == 2:  # inpaint
         image, mask = init_img_with_mask["image"], init_img_with_mask["mask"]
-        mask = mask.split()[-1].convert("L").point(lambda x: 255 if x > 128 else 0)
+        mask = processing.create_binary_mask(mask)
         image = image.convert("RGB")
     elif mode == 3:  # inpaint sketch
         image = inpaint_color_sketch
diff --git a/modules/processing.py b/modules/processing.py
--- a/modules/processing.py
+++ b/modules/processing.py
@@ -81,6 +81,12 @@ def apply_overlay(image, paste_loc, index, overlays):
 
     return image
 
+def create_binary_mask(image):
+    if image.mode == 'RGBA' and image.getextrema()[-1] != (255, 255):
+        image = image.split()[-1].convert("L").point(lambda x: 255 if x > 128 else 0)
+    else:
+        image = image.convert('L')
+    return image
 
 def txt2img_image_conditioning(sd_model, x, width, height):
     if sd_model.model.conditioning_key in {'hybrid', 'concat'}: # Inpainting models
@@ -1385,7 +1391,9 @@ def init(self, all_prompts, all_seeds, all_subseeds):
         image_mask = self.image_mask
 
         if image_mask is not None:
-            image_mask = image_mask.convert('L')
+            # image_mask is passed in as RGBA by Gradio to support alpha masks,
+            # but we still want to support binary masks.
+            image_mask = create_binary_mask(image_mask)
 
             if self.inpainting_mask_invert:
                 image_mask = ImageOps.invert(image_mask)
diff --git a/modules/ui.py b/modules/ui.py
--- a/modules/ui.py
+++ b/modules/ui.py
@@ -598,7 +598,7 @@ def update_orig(image, state):
 
                     with gr.TabItem('Inpaint upload', id='inpaint_upload', elem_id="img2img_inpaint_upload_tab") as tab_inpaint_upload:
                         init_img_inpaint = gr.Image(label="Image for img2img", show_label=False, source="upload", interactive=True, type="pil", elem_id="img_inpaint_base")
-                        init_mask_inpaint = gr.Image(label="Mask", source="upload", interactive=True, type="pil", elem_id="img_inpaint_mask")
+                        init_mask_inpaint = gr.Image(label="Mask", source="upload", interactive=True, type="pil", image_mode="RGBA", elem_id="img_inpaint_mask")
 
                     with gr.TabItem('Batch', id='batch', elem_id="img2img_batch_tab") as tab_batch:
                         hidden = '<br>Disabled when launched with --hide-ui-dir-config.' if shared.cmd_opts.hide_ui_dir_config else ''


Test patch: