Comfy-Org / Comfy-Org/docs

image properties and handling tensors with PIL and cv2

Open
#68 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
MDX
Stars
288
Forks
206
Avg merge
1d 8h
Merged PRs (30d)
135

Description

https://docs.comfy.org/custom-nodes/backend/images_and_masks
https://docs.comfy.org/custom-nodes/backend/tensors

what I'm missing is:
* what is the value range? hidden in here I can derive it's 0..1 not 0..255 https://docs.comfy.org/custom-nodes/backend/snippets
* examples of image batches and pillow (iterate over batch, convert from tensor to pil, convert from pil to tensor)
* examples of image batches and cv2 (iterate over batch, convert from tensor to nparray, convert from nparray to tensor)
* any utility functions which already provide this

LoadImage:
```python
def load_image(self, image):
image_path = folder_paths.get_annotated_filepath(image)

img = node_helpers.pillow(Image.open, image_path)

output_images = []
output_masks = []
w, h = None, None

excluded_formats = ['MPO']

for i in ImageSequence.Iterator(img):
i = node_helpers.pillow(ImageOps.exif_transpose, i)

if i.mode == 'I':
i = i.point(lambda i: i * (1 / 255))
image = i.convert("RGB")

if len(output_images) == 0:
w = image.size[0]
h = image.size[1]

if image.size[0] != w or image.size[1] != h:
continue

image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
if 'A' in i.getbands():
mask = np.array(i.getchannel('A')).astype(np.float32) / 255.0
mask = 1. - torch.from_numpy(mask)
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
output_images.append(image)
output_masks.append(mask.unsqueeze(0))

if len(output_images) > 1 and img.format not in excluded_formats:
output_image = torch.cat(output_images, dim=0)
output_mask = torch.cat(output_masks, dim=0)
else:
output_image = output_images[0]
output_mask = output_masks[0]

return (output_image, output_mask)
```

SaveImage:
```python
def save_images(self, images, 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()
for (batch_number, image) in enumerate(images):
i = 255. * image.cpu().numpy()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
metadata = None
if not args.disable_metadata:
metadata = PngInfo()
if prompt is not None:
metadata.add_text("prompt", json.dumps(prompt))
if extra_pnginfo is not None:
for x in extra_pnginfo:
metadata.add_text(x, json.dumps(extra_pnginfo[x]))

filename_with_batch_num = filename.replace("%batch_num%", str(batch_number))
file = f"{filename_with_batch_num}_{counter:05}_.png"
img.save(os.path.join(full_output_folder, file), pnginfo=metadata, compress_level=self.compress_level)
results.append({
"filename": file,
"subfolder": subfolder,
"type": self.type
})
counter += 1

return { "ui": { "images": results } }```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.