Optimize Dumas image compare conversion path
This commit is contained in:
@@ -7,6 +7,19 @@ from PIL import Image
|
|||||||
import folder_paths
|
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:
|
class DumasImageCompareNode:
|
||||||
DESCRIPTION = (
|
DESCRIPTION = (
|
||||||
"Dumas Image Compare shows the difference between two images directly on "
|
"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):
|
def compare_images(self, image1=None, image2=None):
|
||||||
pairs = ((1, image1), (2, image2))
|
present = []
|
||||||
present = [(slot, tensor) for slot, tensor in pairs if tensor is not None]
|
if image1 is not None:
|
||||||
|
present.append((1, image1))
|
||||||
|
if image2 is not None:
|
||||||
|
present.append((2, image2))
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
if present:
|
if present:
|
||||||
first_tensor = present[0][1]
|
first_tensor = present[0][1]
|
||||||
prefix = "dumas_compare" + self.prefix_append
|
prefix = "dumas_compare" + self.prefix_append
|
||||||
|
first_image = first_tensor[0]
|
||||||
full_output_folder, filename, counter, subfolder, _ = folder_paths.get_save_image_path(
|
full_output_folder, filename, counter, subfolder, _ = folder_paths.get_save_image_path(
|
||||||
prefix,
|
prefix,
|
||||||
self.output_dir,
|
self.output_dir,
|
||||||
first_tensor[0].shape[1],
|
first_image.shape[1],
|
||||||
first_tensor[0].shape[0],
|
first_image.shape[0],
|
||||||
)
|
)
|
||||||
|
join_path = os.path.join
|
||||||
for slot, tensor in present:
|
for slot, tensor in present:
|
||||||
image_array = 255.0 * tensor[0].cpu().numpy()
|
image = _tensor_image_to_pil_image(tensor)
|
||||||
image = Image.fromarray(np.clip(image_array, 0, 255).astype(np.uint8))
|
|
||||||
file_name = f"{filename}_{counter:05}_.png"
|
file_name = f"{filename}_{counter:05}_.png"
|
||||||
image.save(
|
image.save(
|
||||||
os.path.join(full_output_folder, file_name),
|
join_path(full_output_folder, file_name),
|
||||||
compress_level=self.compress_level,
|
compress_level=self.compress_level,
|
||||||
)
|
)
|
||||||
results.append(
|
results.append(
|
||||||
|
|||||||
Reference in New Issue
Block a user