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

Provide option to use bilinear filtering for resizing masks/grayscale images #16347

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

LoganBooker
Copy link
Contributor

Description

The fallback resizing option in resize_image is Lanczos, which is a good default in the majority of cases. However, when resizing grayscale images, in particular masks, it can introduce ringing artifacts. A better option in this case is bilinear filtering. You can see the difference in the filters in the provided image (which are outputs of the cropping masks used in img2img during ADetailer processing with mask blurring set to zero).

Both nearest and bicubic were considered, however:

  1. Bicubic can "pinch" hard corners, which are extremely common for binary masks.
  2. Nearest is optimal for binary masks, but inferior if the mask is blurred (which is true in almost all cases).
  3. Therefore, bilinear provides the best compromise.

To this end, this commit introduces a new option in the img2img settings (disabled by default) that allows the use of bilinear filtering when resizing, if the incoming image is grayscale (PIL's mode 'L'). In all other cases, Lanczos is used as is the case currently.

I've made it a toggle because it does alter the shape of the mask enough to potentially affect current workflows. The effect is also very subtle (as evidenced by the image), but I would argue is more correct.

Screenshots/videos:

bilinear_vs_lanczos

Checklist:

…ault)

* Lanczos is a good default when resizing, however, it is suboptimal for masks as it can introduce ringing artifacts. So use bilinear in these situations.
* Bicubic is better than Lanczos here, but can introduce "pinching" on corners. Nearest would be optimal for binary masks, but much worse if there's any blurring.
* Adds a new toggle to the img2img settings, which is disabled by default, as it does affect the shape of masks compared to Lanczos.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant