Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: img2img batch should ignore non-image files #9185

Closed
1 task done
liudibo opened this issue Mar 30, 2023 · 3 comments · Fixed by sphuff/stable-diffusion-webui#8
Closed
1 task done
Labels
enhancement New feature or request

Comments

@liudibo
Copy link

liudibo commented Mar 30, 2023

Is there an existing issue for this?

  • 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

@liudibo liudibo added the enhancement New feature or request label Mar 30, 2023
@Z-nonymous
Copy link
Contributor

@liudibo
In img2img.py you can see that for the batch (process_batch()) the contents of directory are retrieved with shared.listfiles(). In 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.

@Z-nonymous
Copy link
Contributor

Z-nonymous commented Mar 31, 2023

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
Copy link
Author

liudibo commented Mar 31, 2023

@liudibo In img2img.py you can see that for the batch (process_batch()) the contents of directory are retrieved with shared.listfiles(). In 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants