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

Merge by mask #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions MaskNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,63 @@ def paste(self, image_base, image_to_paste, mask, resize_behavior, mask_mapping_
else:
paste_mask = torch.min(pasting_alpha, mask[i]).unsqueeze(2).repeat(1, 1, 4)
result[image_index] = pasting * paste_mask + result[image_index] * (1. - paste_mask)

return (result,)


class MergeByMask:
"""
Merge `image_to_paste` onto `image_base` using `mask` to determine the location.
"""
def __init__(self):
pass

@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image_base": ("IMAGE",),
"image_to_paste": ("IMAGE",),
"mask": ("IMAGE",),
},
}

RETURN_TYPES = ("IMAGE",)
FUNCTION = "merge"

CATEGORY = "Masquerade Nodes"

def merge(self, image_base, image_to_paste, mask):
image_to_paste = tensor2rgba(image_to_paste)
B, H, W, C = image_to_paste.shape

image_base = tensor2rgba(image_base)
# batchify the image_base tensor
if image_base.shape[0] == 1:
image_base = image_base.repeat(B, 1, 1, 1)
else:
raise ValueError(
"image_base must be a single image.\n"
f"Here: #(image_base)={image_base.shape[0]}."
)
if image_base.shape[1:] != torch.Size([H,W,C]):
raise ValueError(
"image_base must have the same format as image_to_paste.\n"
f"Here: image_base -> {tuple(image_base.shape[1:])}; image_to_paste -> {(H,W,C)}."
)

mask = tensor2mask(mask)
if mask.shape[1:] != torch.Size([H, W]):
raise ValueError(
"mask must have the same size as image_base (and image_to_paste).\n"
f"Here: mask -> {tuple(mask.shape[1:])}; base_image -> {(H, W)}."
)
mask = mask.unsqueeze(-1).repeat(B, 1, 1, 4)

result = mask * image_to_paste + (1-mask) * image_base

return (result,)


class GetImageSize:
def __init__(self):
Expand Down Expand Up @@ -1333,6 +1389,7 @@ def increment(self, seed, max_value):
"Mask To Region": MaskToRegion,
"Cut By Mask": CutByMask,
"Paste By Mask": PasteByMask,
"Merge By Mask": MergeByMask,
"Get Image Size": GetImageSize,
"Change Channel Count": ChangeChannelCount,
"Constant Mask": ConstantMask,
Expand All @@ -1358,6 +1415,7 @@ def increment(self, seed, max_value):
"Mask To Region": "Mask To Region",
"Cut By Mask": "Cut By Mask",
"Paste By Mask": "Paste By Mask",
"Merge By Mask": "Merge By Mask",
"Get Image Size": "Get Image Size",
"Change Channel Count": "Change Channel Count",
"Constant Mask": "Constant Mask",
Expand Down
Loading