Skip to content

Commit

Permalink
switched image gradient function to run full range of color map
Browse files Browse the repository at this point in the history
EOL whitespace auto-cleaned =l
ignore for vscode .core-workspace files
  • Loading branch information
Amorano committed Jun 27, 2024
1 parent 43039ea commit a743860
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__pycache__/
*.py[cod]
*$py.class
*.code-workspace

# C extensions
*.so
Expand Down
51 changes: 30 additions & 21 deletions WAS_Node_Suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1924,33 +1924,42 @@ def gradient(self, size, mode='horizontal', colors=None, tolerance=0):
return img

# Version 2 optimized based on Mark Setchell's ideas
def gradient_map(self, image, gradient_map, reverse=False):
def gradient_map(self, image, gradient_map_input, reverse=False):

# Reverse the image
if reverse:
gradient_map = gradient_map.transpose(Image.FLIP_LEFT_RIGHT)
gradient_map_input = gradient_map_input.transpose(Image.FLIP_LEFT_RIGHT)

# Convert image to Numpy array and average RGB channels
na = np.array(image)
grey = np.mean(na, axis=2).astype(np.uint8)
# grey = self.greyscale(np.array(image))
grey = np.array(image.convert('L'))

# Convert gradient map to Numpy array
cmap = np.array(gradient_map.convert('RGB'))
cmap = np.array(gradient_map_input.convert('RGB'))

# Make output image, same height and width as grey image, but 3-channel RGB
result = np.zeros((*grey.shape, 3), dtype=np.uint8)
# smush the map into the proper size -- 256 gradient colors
cmap = cv2.resize(cmap, (256, 256))

# Reshape grey to match the shape of result
grey_reshaped = grey.reshape(-1)
# lop off a single row for the LUT mapper
cmap = cmap[0,:,:].reshape((256, 1, 3)).astype(np.uint8)

# Take entries from RGB gradient map according to grayscale values in image
np.take(cmap.reshape(-1, 3), grey_reshaped, axis=0, out=result.reshape(-1, 3))
# map with our "custom" LUT
result = cv2.applyColorMap(grey, cmap)

# Convert result to PIL image
result_image = Image.fromarray(result)

return result_image
return Image.fromarray(result)

def greyscale(self, image):
if image.dtype in [np.float16, np.float32, np.float64]:
image = np.clip(image * 255, 0, 255).astype(np.uint8)
cc = image.shape[2] if image.ndim == 3 else 1
if cc == 1:
return image
typ = cv2.COLOR_BGR2HSV
if cc == 4:
typ = cv2.COLOR_BGRA2GRAY
image = cv2.cvtColor(image, typ)[:,:,2]
return np.expand_dims(image, -1)

# Generate Perlin Noise (Finally in house version)

Expand Down Expand Up @@ -4886,12 +4895,12 @@ def INPUT_TYPES(cls):
return {
"required": {
"hex_color": ("STRING", {"default": "#FF0000"}),
},
},
"optional": {
"include_alpha": ("BOOLEAN", {"default": False})
}
}

RETURN_TYPES = ("INT", "INT", "INT", "FLOAT", "STRING")
RETURN_NAMES = ("hue", "saturation", "lightness", "alpha", "hsl")

Expand All @@ -4902,7 +4911,7 @@ def INPUT_TYPES(cls):
def hex_to_hsl(hex_color, include_alpha=False):
if hex_color.startswith("#"):
hex_color = hex_color[1:]

red = int(hex_color[0:2], 16) / 255.0
green = int(hex_color[2:4], 16) / 255.0
blue = int(hex_color[4:6], 16) / 255.0
Expand Down Expand Up @@ -4932,7 +4941,7 @@ def hex_to_hsl(hex_color, include_alpha=False):

hsl_string = f'hsl({round(hue)}, {round(saturation)}%, {round(luminance)}%)' if not include_alpha else f'hsla({round(hue)}, {round(saturation)}%, {round(luminance)}%, {round(alpha, 2)})'
output = (round(hue), round(saturation), round(luminance), round(alpha, 2), hsl_string)

return output


Expand All @@ -4947,7 +4956,7 @@ def INPUT_TYPES(cls):
"hsl_color": ("STRING", {"default": "hsl(0, 100%, 50%)"}),
}
}

RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("hex_color",)

Expand Down Expand Up @@ -7290,7 +7299,7 @@ def INPUT_TYPES(cls):

RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("images",)

FUNCTION = "was_save_images"

OUTPUT_NODE = True
Expand Down Expand Up @@ -9674,7 +9683,7 @@ def text_multiline(self, text):
new_text = tokens.parseTokens(text)

return (new_text, )


# Text List Concatenate Node

Expand Down

0 comments on commit a743860

Please sign in to comment.