Fix RGBA mask conversion in prepare_mask to preserve transparency #3079
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously, the prepare_mask function used a simple grayscale conversion (mask.convert("L")), which discarded the alpha channel in RGBA images. This resulted in fully transparent pixels being rendered as white, causing them to be mistakenly included in the mask. So, if the user uploaded a PNG mask with transparency in webui's inpaint upload tab, then enabling "Crop input image based on A1111 mask" did not crop at all, since everything was part of the mask.
This commit introduces a custom grayscale conversion that:
• Fully transparent pixels become pure black, • Partially transparent pixels have appropriately scaled grayscale values.
The rest of the function’s behavior (inverting and Gaussian blurring) remains unchanged, preserving the overall mask preparation logic for inpainting tasks.
This update ensures correct handling of PNG masks with transparent pixels by the "Crop input image based on A1111 mask" feature.