Repository: AUTOMATIC1111/stable-diffusion-webui

Issue:
[Feature Request]: img2img batch should ignore non-image files
### Is there an existing issue for this?

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

### What would your feature do ?

I may place some parameter files along the image files in the same folder.
Currently an exception is throw for non-image files when do "Image.open()".

### Proposed workflow

1. Place a txt file along with the png file in folder A
2. Set batch input folder to A
3. Press generate


### Additional information

_No response_


Discussion:
@liudibo 
In [img2img.py](https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/master/modules/img2img.py) you can see that for the batch (`process_batch()`) the contents of directory are retrieved with `shared.listfiles()`. In [shared.py](https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/master/modules/shared.py), you can see that `listfiles()` actually reads all files except those starting with a dot (`.` like Linux hidden files).

So you should start your parameter files with a dot (example: `.parameter.txt`) to avoid this. If you're on Windows it doesn't even hide the files.

It seems also that `listfiles()` is only used to retrieve images in a folder, so maybe it should be corrected to grab only the files with supported image extensions.
If we want to modify `listfile()`, it's probably as simple as adding lines similar to the following before the `return` statement:
```
    imagefilenames = []
    for extension in [".png", "jpg"]:
        imagefilenames += [file for file in filenames if file.lower().endswith(extension)]
```
However file extensions do not guarantee that the file is actually an image, same as no extension does not mean that the file is not an image. And you could also have an image with a unrelated extension.
Also the extension list has to represent all cases of possible image extensions that are supported.

Probably a better way to handle this is to catch the exception `PIL.UnidentifiedImageError` being thrown when using `Image.open()` in `process_batch()`. For instance:

```
        try:
            img = Image.open(image)
        except PIL.UnidentifiedImageError:
            continue
```

Resolving patch:
diff --git a/modules/img2img.py b/modules/img2img.py
--- a/modules/img2img.py
+++ b/modules/img2img.py
@@ -4,7 +4,7 @@
 import traceback
 
 import numpy as np
-from PIL import Image, ImageOps, ImageFilter, ImageEnhance, ImageChops
+from PIL import Image, ImageOps, ImageFilter, ImageEnhance, ImageChops, UnidentifiedImageError
 
 from modules import devices, sd_samplers
 from modules.generation_parameters_copypaste import create_override_settings_dict
@@ -46,7 +46,10 @@ def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args):
         if state.interrupted:
             break
 
-        img = Image.open(image)
+        try:
+            img = Image.open(image)
+        except UnidentifiedImageError:
+            continue
         # Use the EXIF orientation of photos taken by smartphones.
         img = ImageOps.exif_transpose(img)
         p.init_images = [img] * p.batch_size


Test patch: