This commit is contained in:
justumen
2024-08-13 10:57:03 +02:00
parent c36f36e730
commit f5114d5d1a
3 changed files with 13 additions and 14 deletions

View File

@@ -9,7 +9,8 @@ class CombineBackgroundOverlay:
"required": {
"background": ("IMAGE",),
"overlay_alpha": ("IMAGE",),
"position": (["middle", "top", "bottom"],),
"horizontal_position": ("FLOAT", {"default": 50, "min": -50, "max": 150, "step": 0.1}),
"vertical_position": ("FLOAT", {"default": 50, "min": -50, "max": 150, "step": 0.1}),
},
}
@@ -17,7 +18,7 @@ class CombineBackgroundOverlay:
FUNCTION = "combine_background_overlay"
CATEGORY = "Bjornulf"
def combine_background_overlay(self, background, overlay_alpha, position):
def combine_background_overlay(self, background, overlay_alpha, horizontal_position, vertical_position):
# Convert background from torch tensor to numpy array
bg = background[0].numpy()
bg = (bg * 255).astype(np.uint8)
@@ -37,14 +38,11 @@ class CombineBackgroundOverlay:
ov_img = Image.fromarray(ov, 'RGB')
ov_img = ov_img.convert('RGBA')
# Calculate position based on user selection
x = (bg_img.width - ov_img.width) // 2
if position == "middle":
y = (bg_img.height - ov_img.height) // 2
elif position == "top":
y = 0
else: # bottom
y = bg_img.height - ov_img.height
# Calculate horizontal position
x = int((horizontal_position / 100) * (bg_img.width - ov_img.width))
# Calculate vertical position
y = int((vertical_position / 100) * (bg_img.height - ov_img.height))
# Create a new image for this overlay
result = Image.new('RGBA', bg_img.size, (0, 0, 0, 0))
@@ -52,7 +50,7 @@ class CombineBackgroundOverlay:
# Paste the background
result.paste(bg_img, (0, 0))
# Paste the overlay in the selected position
# Paste the overlay in the calculated position
result.paste(ov_img, (x, y), ov_img)
# Convert back to numpy array and then to torch tensor