-
Notifications
You must be signed in to change notification settings - Fork 0
/
krita_image_loader.py
86 lines (75 loc) · 2.68 KB
/
krita_image_loader.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
import base64
import io
import numpy as np
import torch
from PIL import Image, ImageOps
class LoadBase64Image:
'''Node to load images directly from Krita or other external sources
without needing to previously upload a file to inputs directory. Intended
to be used in API only and not the web UI.'''
def __init__(self) -> None:
pass
@classmethod
def INPUT_TYPES(s):
return {
"required":
{"image": ("STRING", {"multiline": True})}
}
RETURN_TYPES = ("IMAGE", "MASK")
CATEGORY = "comfyui_krita_plugin"
FUNCTION = "load_image"
def load_image(self, image):
imgdata = base64.b64decode(image)
i = Image.open(io.BytesIO(imgdata))
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)
class LoadBase64ImageMask:
_color_channels = ["alpha", "red", "green", "blue"]
@classmethod
def INPUT_TYPES(s):
return {
"required":
{"image": ("STRING", {"multiline": True}),
"channel": (s._color_channels, ), }
}
CATEGORY = "comfyui_krita_plugin"
RETURN_TYPES = ("MASK",)
FUNCTION = "load_image"
def load_image(self, image, channel):
imgdata = base64.b64decode(image)
i = Image.open(io.BytesIO(imgdata))
i = ImageOps.exif_transpose(i)
if i.getbands() != ("R", "G", "B", "A"):
i = i.convert("RGBA")
mask = None
c = channel[0].upper()
if c in i.getbands():
mask = np.array(i.getchannel(c)).astype(np.float32) / 255.0
mask = torch.from_numpy(mask)
if c == 'A':
mask = 1. - mask
else:
mask = torch.zeros((64,64), dtype=torch.float32, device="cpu")
return (mask,)
@classmethod
def VALIDATE_INPUTS(s, image, channel): #image parameter is necessary
if channel not in s._color_channels:
return "Invalid color channel: {}".format(channel)
return True
NODE_CLASS_MAPPINGS = {
"LoadBase64Image": LoadBase64Image,
"LoadBase64ImageMask": LoadBase64ImageMask
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"LoadBase64Image": "Load base64 encoded image",
"LoadBase64ImageMask": "Load base64 encoded image mask"
}