diff --git a/dumas_image_nodes.py b/dumas_image_nodes.py index c12e27c..6b57646 100644 --- a/dumas_image_nodes.py +++ b/dumas_image_nodes.py @@ -7,6 +7,19 @@ from PIL import Image import folder_paths +def _tensor_image_to_pil_image(tensor): + image_tensor = tensor[0] + if hasattr(image_tensor, "mul") and hasattr(image_tensor, "clamp"): + image_array = image_tensor.mul(255).clamp(0, 255) + if hasattr(image_array, "byte"): + image_array = image_array.byte() + image_array = image_array.cpu().numpy() + return Image.fromarray(image_array) + + image_array = 255.0 * image_tensor.cpu().numpy() + return Image.fromarray(np.clip(image_array, 0, 255).astype(np.uint8)) + + class DumasImageCompareNode: DESCRIPTION = ( "Dumas Image Compare shows the difference between two images directly on " @@ -54,25 +67,29 @@ class DumasImageCompareNode: } def compare_images(self, image1=None, image2=None): - pairs = ((1, image1), (2, image2)) - present = [(slot, tensor) for slot, tensor in pairs if tensor is not None] + present = [] + if image1 is not None: + present.append((1, image1)) + if image2 is not None: + present.append((2, image2)) results = [] if present: first_tensor = present[0][1] prefix = "dumas_compare" + self.prefix_append + first_image = first_tensor[0] full_output_folder, filename, counter, subfolder, _ = folder_paths.get_save_image_path( prefix, self.output_dir, - first_tensor[0].shape[1], - first_tensor[0].shape[0], + first_image.shape[1], + first_image.shape[0], ) + join_path = os.path.join for slot, tensor in present: - image_array = 255.0 * tensor[0].cpu().numpy() - image = Image.fromarray(np.clip(image_array, 0, 255).astype(np.uint8)) + image = _tensor_image_to_pil_image(tensor) file_name = f"{filename}_{counter:05}_.png" image.save( - os.path.join(full_output_folder, file_name), + join_path(full_output_folder, file_name), compress_level=self.compress_level, ) results.append(