From 138de56fdc7b720cf09fe11ee441ca90a1ddca11 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Fri, 8 Dec 2023 18:37:06 +0100 Subject: [PATCH] Optimize split transparency output for subsequent operations --- .../image_channel/transparency/split_transparency.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/backend/src/packages/chaiNNer_standard/image_channel/transparency/split_transparency.py b/backend/src/packages/chaiNNer_standard/image_channel/transparency/split_transparency.py index e0249096e..02c51ee6d 100644 --- a/backend/src/packages/chaiNNer_standard/image_channel/transparency/split_transparency.py +++ b/backend/src/packages/chaiNNer_standard/image_channel/transparency/split_transparency.py @@ -6,6 +6,7 @@ from nodes.impl.image_utils import as_target_channels from nodes.properties.inputs import ImageInput from nodes.properties.outputs import ImageOutput +from nodes.utils.utils import get_h_w_c from . import node_group @@ -34,6 +35,14 @@ def split_transparency_node(img: np.ndarray) -> tuple[np.ndarray, np.ndarray]: """Split a multi-channel image into separate channels""" + c = get_h_w_c(img)[2] + if c == 3: + # Performance optimization: + # Subsequent operations will be faster since the underlying array will + # be contiguous in memory. The speed up can anything from nothing to + # 5x faster depending on the operation. + return img, np.ones(img.shape[:2], dtype=img.dtype) + img = as_target_channels(img, 4) rgb = img[:, :, :3]