Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
JayLyu committed Jun 20, 2024
1 parent 36f659b commit f51c637
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 46 deletions.
39 changes: 24 additions & 15 deletions BK_ColorSelector.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@

class BK_ColorSelector:

def __init__(self):
pass

@classmethod
def INPUT_TYPES(s):

return {
"required": {
"hex_colors": ("STRING", {
"multiline": True,
"default": "#FF0036, #FF5000, #0065ff, #3D7FFF"
}),
},
"optional": {
"symbol": ("STRING", {
"multiline": False,
"default": ","
Expand All @@ -23,29 +23,38 @@ def INPUT_TYPES(s):
"step": 1,
"display": "number"
}),
# "print_to_screen": (["enable", "disable"],),
},
}
}

CATEGORY = "⭐️Baikong"
RETURN_TYPES = ("STRING",)
FUNCTION = "select_color"
OUTPUT_NODE = True

def select_color(self, hex_colors, split_count, symbol, ):
# if print_to_screen == "enable":
# print(f"""Your input contains:
# hex_colors: {hex_colors}
# split_count: {split_count}
# """)
def select_color(
self,
hex_colors,
symbol: str = ",",
split_count: int = 1,
):
# 将 str 分割成 list
color_list = hex_colors.split(symbol)
# 确保值在有效范围内
split_count = max(0, min(split_count - 1, len(color_list) - 1))
# 提取指定颜色
selected_color = color_list[split_count]

# 去掉多余的空格
selected_color = selected_color.replace(" ", "")

return (selected_color,)

# selector_node = BK_ColorSelector()
# input_colors = "#FF0036, #FF5000, #0065ff, #3D7FFF"
# print(selector_node.select_color(input_colors, 1, ",",True))

if __name__ == "__main__":
selector_node = BK_ColorSelector()
print(
selector_node.select_color(
hex_colors="#FF0036, #FF5000, #0065ff, #3D7FFF",
symbol=",",
split_count=1,
)
)
37 changes: 22 additions & 15 deletions BK_GradientImage.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import torch
import numpy as np
from PIL import Image, ImageDraw
from torchvision.transforms import ToPILImage


def pil2tensor(image):
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)

class BK_GradientImage:

def __init__(self):
pass
class BK_GradientImage:

@classmethod
def INPUT_TYPES(s):
Expand Down Expand Up @@ -58,7 +58,7 @@ def main(

r, g, b = int(hex_color[1:3], 16), int(
hex_color[3:5], 16), int(hex_color[5:7], 16)

if direction == 'horizontal':
start_x = int(width * start_position)
midpoint_x = int(width * end_position)
Expand All @@ -76,20 +76,27 @@ def main(
# Fill the rest from midpoint_y to the end with solid color
draw.rectangle([0, midpoint_y, width, height], fill=(r, g, b, 255))

# 旋转图像
if reverse:
image = image.rotate(180)

image_out = pil2tensor(image)

return (image_out,)


# 示例使用
# hex_color = "#34C3EB"
# size = (400, 400)
# start_position = 0.5 # 渐变从10%的位置开始
# end_position = 0.7 # 渐变在90%的位置结束
# direction = 'vertical' # 渐变方向为垂直

# image = generate_gradient_image(
# hex_color, size, start_position, end_position, direction)
# image.show()
if __name__ == "__main__":
BK_GradientImage = BK_GradientImage()
image = BK_GradientImage.main(
hex_color="#34C3EB",
width=512,
height=512,
start_position=0.5,
end_position=1,
direction='vertical',
reverse=False
)
# 把 Tensor 转化回 PIL 图片
image_pil = ToPILImage()(image[0].squeeze(0) * 255).convert('RGBA')
# 显示图片
image_pil.show()
28 changes: 13 additions & 15 deletions BK_Img2Color.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@


class BK_Img2Color:
CATEGORY = "⭐️Baikong"


@classmethod
def INPUT_TYPES(s):

return {
"required": {
"input_image": ("IMAGE",),
},
"optional": {
"num_colors": ("INT", {"default": 3, "min": 1}),
"get_complementary": (
"BOOLEAN",
Expand Down Expand Up @@ -53,17 +55,14 @@ def INPUT_TYPES(s):
"default": "",
},
),
},
}
}

RETURN_TYPES = (
"STRING",
# "IMAGE"
)
# RETURN_NAMES = (
# "hex_colors",
# "preview"
# )
CATEGORY = "⭐️Baikong"
FUNCTION = "main"
OUTPUT_NODE = True

Expand All @@ -76,8 +75,6 @@ def main(
get_complementary: bool = False,
exclude_colors: str = "",
) -> Tuple[str, ...]:

# print(input_image)

if exclude_colors.strip():
self.exclude = exclude_colors.strip().split(",")
Expand All @@ -91,7 +88,8 @@ def main(
self.webcolor_dict.update(webcolors.CSS2_HEX_TO_NAMES,)
self.webcolor_dict.update(webcolors.CSS21_HEX_TO_NAMES,)
self.webcolor_dict.update(webcolors.HTML4_HEX_TO_NAMES,)
print(self.webcolor_dict)

# print(self.webcolor_dict)

original_colors = self.interrogate_colors(input_image, num_colors)
rgb = self.ndarrays_to_rgb(original_colors)
Expand Down Expand Up @@ -159,7 +157,7 @@ def generate_color_blocks(self, color_string: str) -> np.ndarray:
ax.axis('off')

plt.tight_layout()
plt.savefig('d:\\color_blocks.png')
# plt.savefig('d:\\color_blocks.png')

return plt

Expand Down Expand Up @@ -259,7 +257,7 @@ def generate_palette(self, img:torch.Tensor, n_colors=16, cell_size=128, padding

return palette, '\n'.join(hex_palette)


# test = BK_Img2Color()
# input = "E:\ComfyUI\input\ice.png"
# print(test.main(input, 5, "lloyd", 80, False, "", None, None))
if __name__ == "__main__":
BK_Img2Color = BK_Img2Color()
input = "E:\ComfyUI\input\ice.png"
print(BK_Img2Color.main(input, 3, "lloyd", 80, False, ""))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "comfyui_baikong_node"
description = "Nodes:BK Img To Color, BK Color Selector"
version = "1.0.0"
version = "1.0.1"
license = "LICENSE"
dependencies = ["colornamer>=0.2.4", "scikit_learn>=1.4.0", "webcolors==1.11"]

Expand Down

0 comments on commit f51c637

Please sign in to comment.