File size: 6,337 Bytes
1e3b872 |
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
from PIL import Image, ImageEnhance, ImageColor, ImageOps
import numpy as np
import torch
from server import PromptServer
class PreviewTextNode:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"text": ("STRING", {"forceInput": True}),
},
"hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"},
}
RETURN_TYPES = ("STRING",)
OUTPUT_NODE = True
FUNCTION = "preview_text"
CATEGORY = "AlekPet Nodes/extras"
def preview_text(self, text, prompt=None, extra_pnginfo=None):
return {
"ui": {
"string": [
text,
]
},
"result": (text,),
}
# Correction colors nodes
class HexToHueNode:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"color_hex": (
"STRING",
{"default": "#00ff33"},
),
},
"hidden": {"unique_id": "UNIQUE_ID"},
}
RETURN_TYPES = ("STRING", "FLOAT", "FLOAT", "STRING", "STRING")
RETURN_NAMES = (
"string_hex",
"float_hue_degrees",
"float_hue_norm",
"string_hue_degrees",
"string_hue_norm",
)
FUNCTION = "to_hue"
CATEGORY = "AlekPet Nodes/extras"
def to_hue(self, color_hex, unique_id):
hue_degrees = ColorsCorrectNode.hex_to_hue(color_hex)
hue_norm = ColorsCorrectNode.degrees_to_hue(hue_degrees)
PromptServer.instance.send_sync(
"alekpet_get_color_hex", {"color_hex": color_hex, "unique_id": unique_id}
)
return (color_hex, hue_degrees, hue_norm, str(hue_degrees), str(hue_norm))
class ColorsCorrectNode:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"brightness": (
"FLOAT",
{"default": 1.0, "min": 0.0, "max": 100.0, "step": 0.05},
),
"contrast": (
"FLOAT",
{"default": 1.0, "min": 0.0, "max": 100.0, "step": 0.05},
),
"saturation": (
"FLOAT",
{"default": 1.0, "min": 0.0, "max": 100.0, "step": 0.05},
),
"gamma": (
"FLOAT",
{"default": 1.0, "min": 0.0, "max": 100.0, "step": 0.05},
),
"hue_degrees": (
"FLOAT",
{"default": 0.0, "min": 0.0, "max": 360.0, "step": 0.01},
),
"use_color": (
"BOOLEAN",
{"default": True},
),
},
"optional": {
"hex_color": (
"STRING",
{"default": "#00FF33"},
),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "correct"
CATEGORY = "AlekPet Nodes/extras"
@staticmethod
def hex_to_rgb(hex_color):
return ImageColor.getcolor(hex_color, "RGB")
@staticmethod
def hex_to_hue(hex_color):
rgb = ImageColor.getcolor(hex_color, "RGB")
r, g, b = [x / 255.0 for x in rgb]
mx = max(r, g, b)
mn = min(r, g, b)
df = mx - mn
if mx == mn:
h = 0
elif mx == r:
h = (60 * ((g - b) / df) + 360) % 360
elif mx == g:
h = (60 * ((b - r) / df) + 120) % 360
elif mx == b:
h = (60 * ((r - g) / df) + 240) % 360
return h
@staticmethod
def adjust_brightness(image, factor):
enhancer = ImageEnhance.Brightness(image)
return enhancer.enhance(factor)
@staticmethod
def adjust_contrast(image, factor):
enhancer = ImageEnhance.Contrast(image)
return enhancer.enhance(factor)
@staticmethod
def adjust_saturation(image, factor):
enhancer = ImageEnhance.Color(image)
return enhancer.enhance(factor)
@staticmethod
def adjust_gamma(image, gamma):
inv_gamma = 1.0 / gamma
lut = [pow(x / 255.0, inv_gamma) * 255 for x in range(256)]
lut = np.array(lut * 3, dtype=np.uint8)
return image.point(lut)
@staticmethod
def degrees_to_hue(degrees):
degrees = degrees % 360
hue = degrees / 360.0
if hue > 0.5:
hue -= 1.0
if hue < -0.5:
hue += 1.0
return hue
@staticmethod
def adjust_hue(image, hue):
if not (-0.5 <= hue <= 0.5):
raise ValueError("hue value is not in [-0.5, 0.5].")
image_array = np.array(image.convert("RGB"), dtype=np.uint8)
hsv_image = Image.fromarray(image_array).convert("HSV")
hsv_array = np.array(hsv_image)
hsv_array[..., 0] = (hsv_array[..., 0].astype(int) + int(hue * 255)) % 256
rgb_image = Image.fromarray(hsv_array, mode="HSV").convert("RGB")
return rgb_image
@staticmethod
def tint_image(image, hex_color):
return ImageOps.colorize(image.convert("L"), black="black", white=hex_color)
def correct(
self,
image,
use_color=True,
hex_color="#00FF33",
brightness=1.0,
contrast=1.0,
saturation=1.0,
gamma=1.0,
hue_degrees=0.0,
):
i = 255.0 * image[0].cpu().numpy()
image = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
if use_color:
image = ColorsCorrectNode.tint_image(image, hex_color)
image = ColorsCorrectNode.adjust_brightness(image, brightness)
image = ColorsCorrectNode.adjust_contrast(image, contrast)
image = ColorsCorrectNode.adjust_saturation(image, saturation)
image = ColorsCorrectNode.adjust_gamma(image, gamma)
hue_norm = ColorsCorrectNode.degrees_to_hue(hue_degrees)
image = ColorsCorrectNode.adjust_hue(image, hue_norm)
image = ImageOps.exif_transpose(image)
image = image.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
return (image,)
|