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

Image2Image fit option blends pixels when scaling down which ruins mask edges #625

Closed
mavispuford opened this issue Sep 16, 2022 · 5 comments
Labels
todo tracker Add this to https://github.com/lstein/stable-diffusion/discussions/195

Comments

@mavispuford
Copy link

mavispuford commented Sep 16, 2022

Describe your environment

  • GPU: RTX 3080 founders edition with i9 9900k
  • VRAM: 12
  • CPU arch: x86
  • OS: Windows
  • Python: miniconda
  • Branch: main
  • Commit: 89da371

Describe the bug
When passing a masked image with "fit" enabled, it scales down the image and blends the pixels in the process. This causes artifacts near the edges in the output. Maybe there should be a "nearest neighbor" scaling option?

To Reproduce
Images:
Source image:
person-girl-woman-hair-photography-portrait-108386-pxhere com (1)
Large masked image (2496x1664):
Mask-637989379517936043
Small masked image (960x640):
Mask-637989379593202687

Steps to reproduce the behavior:

  1. Enter a prompt ("a frowning woman") using the large masked image above, default settings (50 steps, strength .75) but with width at 960 and height at 640
  2. Notice the seams around the masked area
  3. Now try it with the small masked image
  4. Notice that it is seamless

Expected behavior
There should be no seams/artifacts in the masked area. The resulting image should be seamless

Screenshots
Large image output:
000374 3113900562-artifacts

Small image output:
000375 820978995-seamless

Additional context
Could you add a nearest neighbor/non-filtered scaling option so the pixels don't get blurred? I like the "fit" option because I don't have to resize images on my own, so it would be nice if it handled masked images well.

@lstein
Copy link
Collaborator

lstein commented Sep 16, 2022

I see the problem. Would the PIL.Image.Resampling.NEAREST resampling filter be the best choice here?

@lstein lstein added the todo tracker Add this to https://github.com/lstein/stable-diffusion/discussions/195 label Sep 16, 2022
@mavispuford
Copy link
Author

@lstein Seems like that would do it. There's definitely a quality difference when using nearest neighbor vs the other methods, though, so it'd probably be best as a parameter. Either that or I suppose you could switch to nearest neighbor when transparent pixels/inpainting is detected.

@lstein
Copy link
Collaborator

lstein commented Sep 17, 2022

I'll give it a try as soon as I get access to my development environment again. (It's an HPC cluster down for maintenance over the weekend). In the meantime, if you'd like to swap the nearest resampling in, just open the file ldm/dream/image_util.py, look for the line resized_image = self.image.resize((rw,rh),resample=Image.Resampling.LANCZOS) and change the resampling method to Image.Resampling.NEAREST

@lstein lstein closed this as completed Sep 17, 2022
@lstein lstein reopened this Sep 17, 2022
@mavispuford
Copy link
Author

Seems to work for me. Here's my output using the large masked image with 960x960 chosen as the size:
a_sad_woman-1228974340-637991001671014705-0

lstein added a commit that referenced this issue Sep 24, 2022
- change image resampling method for mask shrinkage to prevent
  artifacts at edge of mask

Addresses #625
@lstein
Copy link
Collaborator

lstein commented Sep 24, 2022

This should be fixed now.

@lstein lstein closed this as completed Sep 24, 2022
austinbrown34 pushed a commit to cognidesign/InvokeAI that referenced this issue Dec 30, 2022
- change image resampling method for mask shrinkage to prevent
  artifacts at edge of mask

Addresses invoke-ai#625
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
todo tracker Add this to https://github.com/lstein/stable-diffusion/discussions/195
Projects
None yet
Development

No branches or pull requests

2 participants