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
```
> @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.

Thanks for the reply an fix. Using "." as the file prefix seems a proper solution.
By the way, the try/except fix is more helpful for the users who are not familiar with the code base.

Resolving patch:
diff --git a/modules/img2img.py b/modules/img2img.py
--- a/modules/img2img.py
+++ b/modules/img2img.py
@@ -48,7 +48,8 @@ def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args):
 
         try:
             img = Image.open(image)
-        except UnidentifiedImageError:
+        except UnidentifiedImageError as e:
+            print(e)
             continue
         # Use the EXIF orientation of photos taken by smartphones.
         img = ImageOps.exif_transpose(img)


Test patch: