|
| 1 | +from comfy_extras.nodes_mask import ImageCompositeMasked |
| 2 | +import torch |
| 3 | + |
| 4 | +class ImageCompositeWatermark(ImageCompositeMasked): |
| 5 | + @classmethod |
| 6 | + def INPUT_TYPES(s): |
| 7 | + return { |
| 8 | + "required": { |
| 9 | + "destination": ("IMAGE",), |
| 10 | + "watermark": ("IMAGE",), |
| 11 | + "position": (["bottom_right", "bottom_center", "bottom_left"], {"default": "bottom_right"}), |
| 12 | + "resize_ratio": ("FLOAT", {"default": 1, "min": 0, "max": 10, "step": 0.1}), |
| 13 | + "margin": ("INT", {"default": 0, "min": 0, "max": 1000, "step": 1}), |
| 14 | + }, |
| 15 | + "optional": { |
| 16 | + "mask": ("MASK",), |
| 17 | + "enabled": ("BOOLEAN", {"default": True, "label_on": "enabled", "label_off": "disabled"}), |
| 18 | + "invert_mask": ("BOOLEAN", {"default": False, "label_on": "enabled", "label_off": "disabled"}), |
| 19 | + } |
| 20 | + } |
| 21 | + RETURN_TYPES = ("IMAGE",) |
| 22 | + FUNCTION = "composite_watermark" |
| 23 | + CATEGORY = "utils/image" |
| 24 | + |
| 25 | + def composite_watermark(self, destination, watermark, position, resize_ratio, margin, mask=None, enabled=True, invert_mask=False): |
| 26 | + if not enabled: |
| 27 | + return (destination,) |
| 28 | + |
| 29 | + if resize_ratio != 1: |
| 30 | + watermark = torch.nn.functional.interpolate( |
| 31 | + watermark.movedim(-1, 1), scale_factor=resize_ratio, mode="bicubic", antialias=True).movedim(1, -1).clamp(0.0, 1.0) |
| 32 | + if mask is not None: |
| 33 | + mask = torch.nn.functional.interpolate(mask.unsqueeze( |
| 34 | + 0), scale_factor=resize_ratio, mode="bicubic", antialias=True).squeeze(0).clamp(0.0, 1.0) |
| 35 | + |
| 36 | + |
| 37 | + # 计算水印的位置 |
| 38 | + dest_h, dest_w = destination.shape[1:3] |
| 39 | + water_h, water_w = watermark.shape[1:3] |
| 40 | + |
| 41 | + # 计算y坐标 - 总是在底部 |
| 42 | + y = dest_h - water_h - margin |
| 43 | + |
| 44 | + x = 0 |
| 45 | + # 根据position计算x坐标 |
| 46 | + if position == "bottom_left": |
| 47 | + x = margin |
| 48 | + elif position == "bottom_center": |
| 49 | + x = (dest_w - water_w) // 2 |
| 50 | + elif position == "bottom_right": |
| 51 | + x = dest_w - water_w - margin |
| 52 | + |
| 53 | + if invert_mask and mask is not None: |
| 54 | + mask = 1.0 - mask |
| 55 | + |
| 56 | + |
| 57 | + return self.composite(destination, watermark, x, y, False, mask) |
| 58 | + |
| 59 | + |
| 60 | +NODE_CLASS_MAPPINGS = { |
| 61 | + |
| 62 | + #image |
| 63 | + "ImageCompositeWatermark": ImageCompositeWatermark, |
| 64 | +} |
| 65 | + |
| 66 | +NODE_DISPLAY_NAME_MAPPINGS = { |
| 67 | + # Image |
| 68 | + "ImageCompositeWatermark": "Image Composite Watermark", |
| 69 | +} |
0 commit comments