2023-11-18 09:44:17 +00:00
|
|
|
import nodes
|
2023-11-23 18:55:29 +00:00
|
|
|
import folder_paths
|
|
|
|
from comfy.cli_args import args
|
|
|
|
|
|
|
|
from PIL import Image
|
2023-11-24 16:12:10 +00:00
|
|
|
from PIL.PngImagePlugin import PngInfo
|
|
|
|
|
2023-11-23 18:55:29 +00:00
|
|
|
import numpy as np
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
2023-11-18 09:44:17 +00:00
|
|
|
MAX_RESOLUTION = nodes.MAX_RESOLUTION
|
|
|
|
|
|
|
|
class ImageCrop:
|
|
|
|
@classmethod
|
|
|
|
def INPUT_TYPES(s):
|
|
|
|
return {"required": { "image": ("IMAGE",),
|
|
|
|
"width": ("INT", {"default": 512, "min": 1, "max": MAX_RESOLUTION, "step": 1}),
|
|
|
|
"height": ("INT", {"default": 512, "min": 1, "max": MAX_RESOLUTION, "step": 1}),
|
|
|
|
"x": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}),
|
|
|
|
"y": ("INT", {"default": 0, "min": 0, "max": MAX_RESOLUTION, "step": 1}),
|
|
|
|
}}
|
|
|
|
RETURN_TYPES = ("IMAGE",)
|
|
|
|
FUNCTION = "crop"
|
|
|
|
|
|
|
|
CATEGORY = "image/transform"
|
|
|
|
|
|
|
|
def crop(self, image, width, height, x, y):
|
|
|
|
x = min(x, image.shape[2] - 1)
|
|
|
|
y = min(y, image.shape[1] - 1)
|
|
|
|
to_x = width + x
|
|
|
|
to_y = height + y
|
|
|
|
img = image[:,y:to_y, x:to_x, :]
|
|
|
|
return (img,)
|
|
|
|
|
2023-11-19 11:09:01 +00:00
|
|
|
class RepeatImageBatch:
|
|
|
|
@classmethod
|
|
|
|
def INPUT_TYPES(s):
|
|
|
|
return {"required": { "image": ("IMAGE",),
|
|
|
|
"amount": ("INT", {"default": 1, "min": 1, "max": 64}),
|
|
|
|
}}
|
|
|
|
RETURN_TYPES = ("IMAGE",)
|
|
|
|
FUNCTION = "repeat"
|
|
|
|
|
|
|
|
CATEGORY = "image/batch"
|
|
|
|
|
|
|
|
def repeat(self, image, amount):
|
|
|
|
s = image.repeat((amount, 1,1,1))
|
|
|
|
return (s,)
|
2023-11-18 09:44:17 +00:00
|
|
|
|
2023-11-23 18:55:29 +00:00
|
|
|
class SaveAnimatedWEBP:
|
|
|
|
def __init__(self):
|
|
|
|
self.output_dir = folder_paths.get_output_directory()
|
|
|
|
self.type = "output"
|
|
|
|
self.prefix_append = ""
|
|
|
|
|
|
|
|
methods = {"default": 4, "fastest": 0, "slowest": 6}
|
|
|
|
@classmethod
|
|
|
|
def INPUT_TYPES(s):
|
|
|
|
return {"required":
|
|
|
|
{"images": ("IMAGE", ),
|
|
|
|
"filename_prefix": ("STRING", {"default": "ComfyUI"}),
|
|
|
|
"fps": ("FLOAT", {"default": 6.0, "min": 0.01, "max": 1000.0, "step": 0.01}),
|
|
|
|
"lossless": ("BOOLEAN", {"default": True}),
|
|
|
|
"quality": ("INT", {"default": 80, "min": 0, "max": 100}),
|
|
|
|
"method": (list(s.methods.keys()),),
|
|
|
|
# "num_frames": ("INT", {"default": 0, "min": 0, "max": 8192}),
|
|
|
|
},
|
|
|
|
"hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_TYPES = ()
|
|
|
|
FUNCTION = "save_images"
|
|
|
|
|
|
|
|
OUTPUT_NODE = True
|
|
|
|
|
|
|
|
CATEGORY = "_for_testing"
|
|
|
|
|
|
|
|
def save_images(self, images, fps, filename_prefix, lossless, quality, method, num_frames=0, prompt=None, extra_pnginfo=None):
|
2023-11-23 20:06:35 +00:00
|
|
|
method = self.methods.get(method)
|
2023-11-23 18:55:29 +00:00
|
|
|
filename_prefix += self.prefix_append
|
|
|
|
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir, images[0].shape[1], images[0].shape[0])
|
|
|
|
results = list()
|
|
|
|
pil_images = []
|
|
|
|
for image in images:
|
|
|
|
i = 255. * image.cpu().numpy()
|
|
|
|
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
|
|
|
|
pil_images.append(img)
|
|
|
|
|
2023-11-23 20:06:35 +00:00
|
|
|
metadata = pil_images[0].getexif()
|
2023-11-23 18:55:29 +00:00
|
|
|
if not args.disable_metadata:
|
|
|
|
if prompt is not None:
|
|
|
|
metadata[0x0110] = "prompt:{}".format(json.dumps(prompt))
|
|
|
|
if extra_pnginfo is not None:
|
|
|
|
inital_exif = 0x010f
|
|
|
|
for x in extra_pnginfo:
|
|
|
|
metadata[inital_exif] = "{}:{}".format(x, json.dumps(extra_pnginfo[x]))
|
|
|
|
inital_exif -= 1
|
|
|
|
|
|
|
|
if num_frames == 0:
|
|
|
|
num_frames = len(pil_images)
|
|
|
|
|
|
|
|
c = len(pil_images)
|
|
|
|
for i in range(0, c, num_frames):
|
|
|
|
file = f"{filename}_{counter:05}_.webp"
|
|
|
|
pil_images[i].save(os.path.join(full_output_folder, file), save_all=True, duration=int(1000.0/fps), append_images=pil_images[i + 1:i + num_frames], exif=metadata, lossless=lossless, quality=quality, method=method)
|
|
|
|
results.append({
|
|
|
|
"filename": file,
|
|
|
|
"subfolder": subfolder,
|
|
|
|
"type": self.type
|
|
|
|
})
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
animated = num_frames != 1
|
|
|
|
return { "ui": { "images": results, "animated": (animated,) } }
|
|
|
|
|
2023-11-24 16:12:10 +00:00
|
|
|
class SaveAnimatedPNG:
|
|
|
|
def __init__(self):
|
|
|
|
self.output_dir = folder_paths.get_output_directory()
|
|
|
|
self.type = "output"
|
|
|
|
self.prefix_append = ""
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def INPUT_TYPES(s):
|
|
|
|
return {"required":
|
|
|
|
{"images": ("IMAGE", ),
|
|
|
|
"filename_prefix": ("STRING", {"default": "ComfyUI"}),
|
2023-11-24 16:19:23 +00:00
|
|
|
"fps": ("FLOAT", {"default": 6.0, "min": 0.01, "max": 1000.0, "step": 0.01}),
|
2023-11-24 16:12:10 +00:00
|
|
|
"compress_level": ("INT", {"default": 4, "min": 0, "max": 9})
|
|
|
|
},
|
|
|
|
"hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN_TYPES = ()
|
|
|
|
FUNCTION = "save_images"
|
|
|
|
|
|
|
|
OUTPUT_NODE = True
|
|
|
|
|
|
|
|
CATEGORY = "_for_testing"
|
|
|
|
|
|
|
|
def save_images(self, images, fps, compress_level, filename_prefix="ComfyUI", prompt=None, extra_pnginfo=None):
|
|
|
|
filename_prefix += self.prefix_append
|
|
|
|
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir, images[0].shape[1], images[0].shape[0])
|
|
|
|
results = list()
|
|
|
|
pil_images = []
|
|
|
|
for image in images:
|
|
|
|
i = 255. * image.cpu().numpy()
|
|
|
|
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
|
|
|
|
pil_images.append(img)
|
|
|
|
|
|
|
|
metadata = None
|
|
|
|
if not args.disable_metadata:
|
|
|
|
metadata = PngInfo()
|
|
|
|
if prompt is not None:
|
2023-11-24 23:14:17 +00:00
|
|
|
metadata.add(b"tEXt", "prompt".encode("latin-1", "strict") + b"\0" + json.dumps(prompt).encode("latin-1", "strict"), after_idat=True)
|
2023-11-24 16:12:10 +00:00
|
|
|
if extra_pnginfo is not None:
|
|
|
|
for x in extra_pnginfo:
|
2023-11-24 23:14:17 +00:00
|
|
|
metadata.add(b"tEXt", x.encode("latin-1", "strict") + b"\0" + json.dumps(extra_pnginfo[x]).encode("latin-1", "strict"), after_idat=True)
|
2023-11-24 16:12:10 +00:00
|
|
|
|
|
|
|
file = f"{filename}_{counter:05}_.png"
|
|
|
|
pil_images[0].save(os.path.join(full_output_folder, file), pnginfo=metadata, compress_level=compress_level, save_all=True, duration=int(1000.0/fps), append_images=pil_images[1:])
|
|
|
|
results.append({
|
|
|
|
"filename": file,
|
|
|
|
"subfolder": subfolder,
|
|
|
|
"type": self.type
|
|
|
|
})
|
|
|
|
|
|
|
|
return { "ui": { "images": results, "animated": (True,)} }
|
|
|
|
|
2023-11-18 09:44:17 +00:00
|
|
|
NODE_CLASS_MAPPINGS = {
|
|
|
|
"ImageCrop": ImageCrop,
|
2023-11-19 11:09:01 +00:00
|
|
|
"RepeatImageBatch": RepeatImageBatch,
|
2023-11-23 18:55:29 +00:00
|
|
|
"SaveAnimatedWEBP": SaveAnimatedWEBP,
|
2023-11-24 16:12:10 +00:00
|
|
|
"SaveAnimatedPNG": SaveAnimatedPNG,
|
2023-11-18 09:44:17 +00:00
|
|
|
}
|