mirror of
https://github.com/justUmen/Bjornulf_custom_nodes.git
synced 2026-03-21 20:52:11 -03:00
fix pingpong, new node combine image background + overlay
This commit is contained in:
66
combine_background_overlay.py
Normal file
66
combine_background_overlay.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import torch
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
class CombineBackgroundOverlay:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {
|
||||
"required": {
|
||||
"background": ("IMAGE",),
|
||||
"overlay_alpha": ("IMAGE",),
|
||||
},
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
FUNCTION = "combine_background_overlay"
|
||||
CATEGORY = "Bjornulf"
|
||||
|
||||
def combine_background_overlay(self, background, overlay_alpha):
|
||||
# Convert background from torch tensor to numpy array
|
||||
bg = background[0].numpy()
|
||||
bg = (bg * 255).astype(np.uint8)
|
||||
bg_img = Image.fromarray(bg, 'RGB')
|
||||
|
||||
results = []
|
||||
|
||||
for overlay in overlay_alpha:
|
||||
# Convert overlay from torch tensor to numpy array
|
||||
ov = overlay.numpy()
|
||||
ov = (ov * 255).astype(np.uint8)
|
||||
|
||||
# Create PIL Image for overlay
|
||||
if ov.shape[2] == 4:
|
||||
ov_img = Image.fromarray(ov, 'RGBA')
|
||||
else:
|
||||
ov_img = Image.fromarray(ov, 'RGB')
|
||||
ov_img = ov_img.convert('RGBA')
|
||||
|
||||
# Calculate position to center the overlay
|
||||
x = (bg_img.width - ov_img.width) // 2
|
||||
y = (bg_img.height - ov_img.height) // 2
|
||||
|
||||
# Create a new image for this overlay
|
||||
result = Image.new('RGBA', bg_img.size, (0, 0, 0, 0))
|
||||
|
||||
# Paste the background
|
||||
result.paste(bg_img, (0, 0))
|
||||
|
||||
# Paste the overlay in the center
|
||||
result.paste(ov_img, (x, y), ov_img)
|
||||
|
||||
# Convert back to numpy array and then to torch tensor
|
||||
result_np = np.array(result)
|
||||
|
||||
# If the result is RGBA, convert to RGB
|
||||
if result_np.shape[2] == 4:
|
||||
result_np = result_np[:,:,:3]
|
||||
|
||||
result_tensor = torch.from_numpy(result_np).float() / 255.0
|
||||
|
||||
results.append(result_tensor)
|
||||
|
||||
# Stack all results into a single tensor
|
||||
final_result = torch.stack(results)
|
||||
|
||||
return (final_result,)
|
||||
Reference in New Issue
Block a user