-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathimage.py
148 lines (118 loc) · 4.18 KB
/
image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import torch
import hashlib
from pathlib import Path
from typing import Iterable
from PIL import Image, ImageOps
import numpy as np
import folder_paths
class LoadImageFromPath:
@classmethod
def INPUT_TYPES(s):
return {"required":
{"image": ("STRING", {"default": r"ComfyUI_00001_-assets\ComfyUI_00001_.png [output]"})},
}
CATEGORY = "image"
RETURN_TYPES = ("IMAGE", "MASK")
FUNCTION = "load_image"
def load_image(self, image):
image_path = LoadImageFromPath._resolve_path(image)
i = Image.open(image_path)
i = ImageOps.exif_transpose(i)
image = i.convert("RGB")
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")
return (image, mask)
def _resolve_path(image) -> Path:
image_path = Path(folder_paths.get_annotated_filepath(image))
return image_path
@classmethod
def IS_CHANGED(s, image):
image_path = LoadImageFromPath._resolve_path(image)
m = hashlib.sha256()
with open(image_path, 'rb') as f:
m.update(f.read())
return m.digest().hex()
@classmethod
def VALIDATE_INPUTS(s, image):
# If image is an output of another node, it will be None during validation
if image is None:
return True
image_path = LoadImageFromPath._resolve_path(image)
if not image_path.exists():
return "Invalid image path: {}".format(image_path)
return True
class PILToImage:
@classmethod
def INPUT_TYPES(s):
return {'required':
{'images': ('PIL_IMAGE', )},
}
RETURN_TYPES = ('IMAGE',)
FUNCTION = 'pil_images_to_images'
CATEGORY = 'image/PIL'
def pil_images_to_images(self, images: Iterable[Image.Image]) -> torch.Tensor:
pil_images = images
images = []
for pil_image in pil_images:
i = pil_image
i = ImageOps.exif_transpose(i)
if i.mode == 'I':
i = i.point(lambda i: i * (1 / 255))
image = i.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
images.append(image)
if len(images) > 1:
images = torch.cat(images, dim=0)
else:
images = images[0]
return (images,)
class PILToMask:
@classmethod
def INPUT_TYPES(s):
return {'required':
{'images': ('PIL_IMAGE', )},
}
RETURN_TYPES = ('IMAGE',)
FUNCTION = 'pil_images_to_masks'
CATEGORY = 'image/PIL'
def pil_images_to_masks(self, images: Iterable[Image.Image]) -> torch.Tensor:
pil_images = images
masks = []
for pil_image in pil_images:
i = pil_image
i = ImageOps.exif_transpose(i)
if i.mode == 'I':
i = i.point(lambda i: i * (1 / 255))
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")
masks.append(mask)
if len(masks) > 1:
masks = torch.cat(masks, dim=0)
else:
masks = masks[0]
return (masks,)
class ImageToPIL:
@classmethod
def INPUT_TYPES(s):
return {'required':
{'images': ('IMAGE', )},
}
RETURN_TYPES = ('PIL_IMAGE',)
FUNCTION = 'images_to_pil_images'
CATEGORY = 'image/PIL'
def images_to_pil_images(self, images: torch.Tensor) -> list[Image.Image]:
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)
return (pil_images,)