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

Antialiasing + finegrained settings for IMAGE OVERLAY #246

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 21 additions & 15 deletions efficiency_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3892,13 +3892,13 @@ def INPUT_TYPES(cls):
"overlay_image": ("IMAGE",),
"overlay_resize": (["None", "Fit", "Resize by rescale_factor", "Resize to width & heigth"],),
"resize_method": (["nearest-exact", "bilinear", "area"],),
"rescale_factor": ("FLOAT", {"default": 1, "min": 0.01, "max": 16.0, "step": 0.1}),
"width": ("INT", {"default": 512, "min": 0, "max": MAX_RESOLUTION, "step": 64}),
"height": ("INT", {"default": 512, "min": 0, "max": MAX_RESOLUTION, "step": 64}),
"x_offset": ("INT", {"default": 0, "min": -48000, "max": 48000, "step": 10}),
"y_offset": ("INT", {"default": 0, "min": -48000, "max": 48000, "step": 10}),
"rotation": ("INT", {"default": 0, "min": -180, "max": 180, "step": 5}),
"opacity": ("FLOAT", {"default": 0, "min": 0, "max": 100, "step": 5}),
"rescale_factor": ("FLOAT", {"default": 1, "min": 0.01, "max": 16.0, "step": 0.01}),
"width": ("INT", {"default": 512, "min": 0, "max": MAX_RESOLUTION, "step": 1}),
"height": ("INT", {"default": 512, "min": 0, "max": MAX_RESOLUTION, "step": 1}),
"x_offset": ("INT", {"default": 0, "min": -48000, "max": 48000, "step": 1}),
"y_offset": ("INT", {"default": 0, "min": -48000, "max": 48000, "step": 1}),
"rotation": ("INT", {"default": 0, "min": -180, "max": 180, "step": 0.1}),
"opacity": ("FLOAT", {"default": 0, "min": 0, "max": 100, "step": 0.1}),
},
"optional": {"optional_mask": ("MASK",),}
}
Expand Down Expand Up @@ -3948,8 +3948,8 @@ def apply_overlay_image(self, base_image, overlay_image, overlay_resize, resize_
# Apply mask as overlay's alpha
overlay_image.putalpha(ImageOps.invert(mask))

# Rotate the overlay image
overlay_image = overlay_image.rotate(rotation, expand=True)
# Rotate the overlay image with antialiasing
overlay_image = overlay_image.rotate(rotation, resample=Image.BICUBIC, expand=True)

# Apply opacity on overlay image
r, g, b, a = overlay_image.split()
Expand All @@ -3965,14 +3965,20 @@ def apply_overlay_image(self, base_image, overlay_image, overlay_resize, resize_
# Convert tensor to PIL Image
image = tensor2pil(tensor)

# Paste the overlay image onto the base image
if mask is None:
image.paste(overlay_image, location)
else:
image.paste(overlay_image, location, overlay_image)
# Create a new blank image with an alpha channel
combined = Image.new('RGBA', image.size, (0, 0, 0, 0))

# Paste the base image onto the new image
combined.paste(image, (0, 0))

# Paste the overlay image onto the combined image using alpha compositing
combined.alpha_composite(overlay_image, location)

# Convert the combined image back to RGB mode
result = combined.convert('RGB')

# Convert PIL Image back to tensor
processed_tensor = pil2tensor(image)
processed_tensor = pil2tensor(result)

# Append to list
processed_base_image_list.append(processed_tensor)
Expand Down