アルファチャンネルをグレースケールに変換するノードと、アルファチャンネルをRGBにフラット化するノードを追加
This commit is contained in:
@@ -4,6 +4,8 @@ from .nodes.binarize_image import BinarizeImage
|
||||
from .nodes.binarize_image_using_otsu import BinarizeImageUsingOtsu
|
||||
from .nodes.antialiasing_image import AntialiasingImage
|
||||
from .nodes.remove_white_background_noise import RemoveWhiteBackgroundNoise
|
||||
from .nodes.alpha_to_grayscale import AlphaToGrayscale
|
||||
from .nodes.alpha_flatten import AlphaFlatten
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"BrightnessTransparency": BrightnessTransparency,
|
||||
@@ -12,6 +14,8 @@ NODE_CLASS_MAPPINGS = {
|
||||
"GrayscaleImage": GrayscaleImage,
|
||||
"AntialiasingImage": AntialiasingImage,
|
||||
"RemoveWhiteBackgroundNoise": RemoveWhiteBackgroundNoise,
|
||||
"AlphaToGrayscale": AlphaToGrayscale,
|
||||
"AlphaFlatten": AlphaFlatten,
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
@@ -21,6 +25,8 @@ NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"GrayscaleImage": "GrayscaleImage",
|
||||
"AntialiasingImage": "AntialiasingImage",
|
||||
"RemoveWhiteBackgroundNoise": "RemoveWhiteBackgroundNoise",
|
||||
"AlphaToGrayscale": "AlphaToGrayscale",
|
||||
"AlphaFlatten": "AlphaFlatten",
|
||||
}
|
||||
|
||||
__all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"]
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
|
||||
class AlphaFlatten:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {
|
||||
"required": {
|
||||
"images": (
|
||||
"IMAGE",
|
||||
{
|
||||
"tooltip": "Input images to flatten alpha channel into RGB colors."
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
FUNCTION = "run"
|
||||
CATEGORY = "ComfyUI-Image-Toolkit"
|
||||
|
||||
def run(self, images):
|
||||
is_tensor = isinstance(images, torch.Tensor)
|
||||
|
||||
if is_tensor:
|
||||
if images.device.type != "cpu":
|
||||
images = images.cpu()
|
||||
images_np = images.numpy()
|
||||
else:
|
||||
images_np = images
|
||||
|
||||
result_images = []
|
||||
|
||||
for image in images_np:
|
||||
# アルファチャンネルがある場合のみ処理
|
||||
if image.shape[2] == 4:
|
||||
# RGB値を取得
|
||||
rgb = image[:, :, :3]
|
||||
# アルファ値を取得
|
||||
alpha = image[:, :, 3]
|
||||
|
||||
# 白背景とのalpha合成処理
|
||||
# result = foreground_color * alpha + background_color * (1 - alpha)
|
||||
# 白背景:background_color = 1.0
|
||||
flattened_rgb = rgb * alpha[:, :, np.newaxis] + 1.0 * (1.0 - alpha[:, :, np.newaxis])
|
||||
|
||||
result_images.append(flattened_rgb)
|
||||
else:
|
||||
# アルファチャンネルがない場合はそのまま返す
|
||||
result_images.append(image)
|
||||
|
||||
result_np = np.stack(result_images)
|
||||
result_tensor = torch.from_numpy(result_np)
|
||||
|
||||
if is_tensor and images.device.type != "cpu":
|
||||
result_tensor = result_tensor.to(images.device)
|
||||
|
||||
return (result_tensor,)
|
||||
@@ -0,0 +1,68 @@
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
|
||||
class AlphaToGrayscale:
|
||||
@classmethod
|
||||
def INPUT_TYPES(s):
|
||||
return {
|
||||
"required": {
|
||||
"images": (
|
||||
"IMAGE",
|
||||
{
|
||||
"tooltip": "Input images to convert alpha to grayscale."
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_TYPES = ("IMAGE",)
|
||||
FUNCTION = "run"
|
||||
CATEGORY = "ComfyUI-Image-Toolkit"
|
||||
|
||||
def run(self, images):
|
||||
is_tensor = isinstance(images, torch.Tensor)
|
||||
|
||||
if is_tensor:
|
||||
if images.device.type != "cpu":
|
||||
images = images.cpu()
|
||||
images_np = images.numpy()
|
||||
else:
|
||||
images_np = images
|
||||
|
||||
result_images = []
|
||||
|
||||
for image in images_np:
|
||||
# アルファチャンネルがある場合のみ処理
|
||||
if image.shape[2] == 4:
|
||||
# アルファ値を取得
|
||||
alpha = image[:, :, 3]
|
||||
|
||||
# アルファ値をグレースケール値に変換(BrightnessTransparencyの逆変換)
|
||||
# BrightnessTransparency: alpha = 1.0 - grayscale
|
||||
# AlphaToGrayscale: grayscale = 1.0 - alpha
|
||||
grayscale = 1.0 - alpha
|
||||
|
||||
# RGB画像を作成(透明度なし)
|
||||
rgb_image = np.zeros(
|
||||
(image.shape[0], image.shape[1], 3), dtype=np.float32
|
||||
)
|
||||
|
||||
# 全チャンネルにグレースケール値を設定
|
||||
rgb_image[:, :, 0] = grayscale
|
||||
rgb_image[:, :, 1] = grayscale
|
||||
rgb_image[:, :, 2] = grayscale
|
||||
|
||||
result_images.append(rgb_image)
|
||||
else:
|
||||
# アルファチャンネルがない場合はそのまま返す
|
||||
result_images.append(image)
|
||||
|
||||
result_np = np.stack(result_images)
|
||||
result_tensor = torch.from_numpy(result_np)
|
||||
|
||||
if is_tensor and images.device.type != "cpu":
|
||||
result_tensor = result_tensor.to(images.device)
|
||||
|
||||
return (result_tensor,)
|
||||
|
||||
Reference in New Issue
Block a user