Fix bug of exceptions caused by empty strings

This commit is contained in:
chflame163
2024-01-14 22:15:13 +08:00
committed by GitHub
parent 73e253e080
commit 376f204fc6
6 changed files with 149 additions and 60 deletions
+62 -58
View File
@@ -1,81 +1,85 @@
# Comfy WordCloud:
ComfyUI生成词云图像的插件。
# ComfyUI Word Cloud:
A plugin to generating word cloud images for ComfyUI
![image](image/comfy_wordcloud_advance.png)
### 更新:
通过编辑插件根目录下的font_dir.ini,用户可以自定义字体目录。每次启动comfyUI时,这个目录下*.ttf和*.otf文件将收集显示在插件font_path选择项中。
[中文说明点这里](./README-CN.md)
font_dir.ini默认是windows系统字体目录(C:\Windows\fonts)。
## Update:
* Add RGB Color Picker nodes makes color selection more convenient.
如果自定义的目录无效,将启用自带font目录,这个目录包含一个Alibaba-PuHuiTi-Heavy.ttf文件,版权归属阿里巴巴(中国)有限公司,允许任何个人和企业免费使用。
增加RGB Color Picker节点,选择颜色更加方便。
### 重要提醒:加载之前工作流保存的旧版节点,需要重新设置字体。
首先设置好font_dir.ini,启动comfyUI并加载工作流,在WordCloud节点的font_path选项栏中重新选择字体,即可避免报错。
* 本次更新基于[ZHO-ZHO-ZHO](https://github.com/ZHO-ZHO-ZHO/ComfyUI-Text_Image-Composite)的建议和帮助。
* By editing the font_dir.ini, located in the root directory of the plugin, users can customize the font directory. Every time comfyUI is launched, the *.ttf and *.otf files
in this directory will be collected and displayed in the plugin font_path option.
font_dir.ini defaults to the Windows system font directory (C:\Windows\fonts).
If the custom directory is invalid, the built-in font directory will be enabled,
This directory contains Alibaba-PuHuiTi-Heavy.ttf file, which belongs to Alibaba (China) Co., Ltd. and is free for use by any individual or enterprise.
节点说明:
### Important reminder: The font needs to be reset for the old version nodes saved in the workflow before loading.
* Set the font_dir.ini, and start comfyUI to load workflow, in the font_path of the WordCloud node, reselect the font.
* This update is based on [ZHO-ZHO-ZHO](https://github.com/ZHO-ZHO-ZHO/ComfyUI-Text_Image-Composite)'s suggestions and assistance.
## Node Description
### Word Cloud:
根据文本内容生成词云图像,词的大小与词频相关,频率越高文字越大。可定义配色,设定重点词,设定排除词等。支持输入带alpha通道的图像生成具有轮廓形状的词云图像。
Generate word cloud images based on text content, where word size is related to word frequency, and the higher the frequency, the larger the text. Can define color schemes, set key words, set exclusion words, etc. Support generating word cloud images with contour shapes by inputting images with alpha channels.
选项说明:
* color_ref_image: 输入的图像作为文字颜色的参考。
* mask_image: 输入的图像作为文字形状的轮廓。如果带alpha通道,使用通道作为轮廓;不带通道的按颜色决定轮廓的形状,白色的部分将被排除。
* text: 此处的文本内容将分解为逐个单词,这些单词作为词云的元素。单词出现频率越高,在词云上描绘得越大。
* width: 生成图像的宽度。(如果有mask_image输入,则使用mask_image的尺寸,此设置数值被忽略)
* height: 生成图像的高度。(如果有mask_image输入,则使用mask_image的尺寸,此设置数值被忽略)
* scale: 放大系数,最终生成的图像大小将是width和height设定数值与此数相乘。
* margin: 空白边大小。
* font_path: 字体文件路径。建议使用ttf格式。
* min_font_size: 单词元素显示的最小值。
* max_font_size:单词元素显示的最大值。
* relative_scaling: 词云中单词元素的相对大小。值越大,离散度越高。
* colormap: 文字颜色。这里使用的是Matplotlib 提供的多种预定义colormap。(如果有color_ref_image输入,则此设置被忽略)
* background_color: 背景颜色,使用16进制RGB格式描述。(如果transparent_background设定为True,则此设置被忽略)
* transparent_background: 背景是否透明。此处设为True将输出带alpha通道的图像。
* prefer_horizontal: 单词水平出现率。最低0(全竖排),最高1(全横排)。
* max_words: 单词最大数量。
* repeat: 当单词没有达到最大数量时是否允许重复。
* include_numbers: 单词元素是否包含数字。
* random_state:控制词云生成过程中随机数生成器的种子。设为-1为每次随机,其他值则每次固定。
* stopwords: 屏蔽词。设定在此处的单词不会在图中出现。每个词之间用逗号(中英文均可)或空格分开。
* contour_width: 轮廓宽度。有mask_image输入才有效。
* contour_color: 轮廓颜色。使用16进制RGB格式描述。有mask_image输入才有效。
* keynote_words: 重点词。这里设定的单词将额外放大,在stopwords中设定了同样单词的除外。每个词之间用逗号(中英文均可)或空格分开。
* keynote_weight: 重点词加权。数值越大,重点词相对越大。
Node options:
* color_ref_image: The input image serves as a reference for text color.
* mask_image: The input image serves as the silhouette of the text shape. If there with alpha channel, use the channel as the contour; The shape of the contour is determined by color without channels, and the white part will be excluded.
* text: The text content here will be broken down into individual words, which serve as elements of the word cloud.
* width: Generate the width of the image. (If there is mask_image input, use the size of mask_image, and this setting value is ignored)
* height: The height of the generated image. (If there is mask_image input, use the size of mask_image, and this setting value is ignored)
* scale: The amplification factor, the final generated image size will be the width and height set values multiplied by this number.
* margin: Blank edge size.
* font_path: font file.
* min_font_size: The minimum value displayed for word elements.
* max_font_size:The maximum value displayed for word elements.
* relative_scaling: The relative size of word elements in the word cloud. The larger the value, the higher the dispersion.
* colormap: Text color. multiple predefined colormaps provided by Matplotlib are used. (If there is input of color_ref_image, this setting is ignored)
* background_color: Background color, described in hexadecimal RGB format. (If transparent background is set to True, this setting is ignored)
* transparent_background: Is the background transparent. Set True here to output images with alpha channels.
* prefer_horizontal: Word level occurrence rate. Minimum 0 (all vertical rows), maximum 1 (all horizontal rows).
* max_words: Maximum number of words.
* repeat: Is it allowed to repeat when the maximum number of words is not reached.
* include_numbers: Does the word element contain numbers.
* random_state:Seed of the random number generator during the word cloud generation process. Set -1 to be random each time, while other values are fixed each time.
* stopwords: The words set here will not appear in the picture. Separate each word with a comma (both in Chinese and English) or a space.
* contour_width: Outline width of silhouette. With a mask_ Image input is only valid.
* contour_color: Color of Outline. With a mask_ Image input is only valid.
* keynote_words: The words set here will be further enlarged, except for those with the same words set in stopwords. Separate each word with a comma (both in Chinese and English) or a space.
* keynote_weight: Weighted key for keynote words. The larger the value, the relatively larger the key words.
输出:
image(支持alpha通道)
Output:
image(support alpha channel)
### RGB Color Picker
来自[mtb nodes](https://github.com/melMass/comfy_mtb)的Web Extensions,在色板上选取颜色,并输出RGB值。
选项说明:
* mode: 输出格式,有十六进制(HEX)和十进制(DEC)可选择。
Modify web extensions from [mtb nodes](https://github.com/melMass/comfy_mtb). Select colors on the color palette and output RGB values.
输出:
string字符串。
Node options:
* mode: The output format is available in hexadecimal (HEX) and decimal (DEC).
Output:
string
### Load Text File:
从指定路径加载文本文件,设定使用utf-8编码。
Load a text file from the specified path and set to use UTF-8 encoding.
选项说明:
* path: txt文件路径。
Options:
* path: Directory for .txt file。
输出:
string字符串。
Output:
string
## 使用示例:
## Example workflow
![image](image/comfy_wordcloud_simple.png)
在workflow目录下有json格式的工作流示例文件。
There is a JSON format workflow example file in the workflow directory.
## 安装方法:
## How to install
* git clone 安装,或者下载解压zip文件,将得到的文件夹复制到 ComfyUI\custom_nodes\
* 安装依赖包,在资源管理器ComfyUI\custom_nodes\ComfyUI_WordCloud 插件目录位置打开cmd窗口,输入以下命令:
* Open the cmd window in the plugin directory of ComfyUI, like "ComfyUI\custom_nodes\",type```git clone https://github.com/chflame163/ComfyUI_WordCloud.git```, or download the zip file and extracted, copy the resulting folder to ComfyUI\custom_ Nodes\
* Install dependency packages in the resource manager ComfyUI\custom_ Nodes\ComfyUI_ Open the cmd window in the WordCloud plugin directory location and enter the following command:
```..\..\..\python_embeded\python.exe -m pip install -r requirements.txt```
* 重新打开ComfyUI。
* restart ComfyUI
+82
View File
@@ -0,0 +1,82 @@
# ComfyUI Word Cloud:
ComfyUI生成词云图像的插件。
![image](image/comfy_wordcloud_advance.png)
## 更新:
* 增加RGB Color Picker节点,选择颜色更加方便。
* 通过编辑插件根目录下的font_dir.ini,用户可以自定义字体目录。每次启动comfyUI时,这个目录下*.ttf和*.otf文件将收集显示在插件font_path选择项中。
font_dir.ini默认是windows系统字体目录(C:\Windows\fonts)。
如果自定义的目录无效,将启用自带font目录,这个目录包含一个Alibaba-PuHuiTi-Heavy.ttf文件,版权归属阿里巴巴(中国)有限公司,允许任何个人和企业免费使用。
### 重要提醒:加载之前工作流保存的旧版节点,需要重新设置字体。
* 首先设置好font_dir.ini,启动comfyUI并加载工作流,在WordCloud节点的font_path选项栏中重新选择字体,即可避免报错。
* 本次更新基于[ZHO-ZHO-ZHO](https://github.com/ZHO-ZHO-ZHO/ComfyUI-Text_Image-Composite)的建议和帮助。
## 节点说明:
### Word Cloud:
根据文本内容生成词云图像,词的大小与词频相关,频率越高文字越大。可定义配色,设定重点词,设定排除词等。支持输入带alpha通道的图像生成具有轮廓形状的词云图像。
选项说明:
* color_ref_image: 输入的图像作为文字颜色的参考。
* mask_image: 输入的图像作为文字形状的轮廓。如果带alpha通道,使用通道作为轮廓;不带通道的按颜色决定轮廓的形状,白色的部分将被排除。
* text: 此处的文本内容将分解为逐个单词,这些单词作为词云的元素。单词出现频率越高,在词云上描绘得越大。
* width: 生成图像的宽度。(如果有mask_image输入,则使用mask_image的尺寸,此设置数值被忽略)
* height: 生成图像的高度。(如果有mask_image输入,则使用mask_image的尺寸,此设置数值被忽略)
* scale: 放大系数,最终生成的图像大小将是width和height设定数值与此数相乘。
* margin: 空白边大小。
* font_path: 字体文件。
* min_font_size: 单词元素显示的最小值。
* max_font_size:单词元素显示的最大值。
* relative_scaling: 词云中单词元素的相对大小。值越大,离散度越高。
* colormap: 文字颜色。这里使用的是Matplotlib 提供的多种预定义colormap。(如果有color_ref_image输入,则此设置被忽略)
* background_color: 背景颜色,使用16进制RGB格式描述。(如果transparent_background设定为True,则此设置被忽略)
* transparent_background: 背景是否透明。此处设为True将输出带alpha通道的图像。
* prefer_horizontal: 单词水平出现率。最低0(全竖排),最高1(全横排)。
* max_words: 单词最大数量。
* repeat: 当单词没有达到最大数量时是否允许重复。
* include_numbers: 单词元素是否包含数字。
* random_state:控制词云生成过程中随机数生成器的种子。设为-1为每次随机,其他值则每次固定。
* stopwords: 屏蔽词。设定在此处的单词不会在图中出现。每个词之间用逗号(中英文均可)或空格分开。
* contour_width: 轮廓宽度。有mask_image输入才有效。
* contour_color: 轮廓颜色。使用16进制RGB格式描述。有mask_image输入才有效。
* keynote_words: 重点词。这里设定的单词将额外放大,在stopwords中设定了同样单词的除外。每个词之间用逗号(中英文均可)或空格分开。
* keynote_weight: 重点词加权。数值越大,重点词相对越大。
输出:
image(支持alpha通道)
### RGB Color Picker
修改自[mtb nodes](https://github.com/melMass/comfy_mtb)的Web Extensions。在色板上选取颜色,并输出RGB值。
选项说明:
* mode: 输出格式,有十六进制(HEX)和十进制(DEC)可选择。
输出:
string字符串。
### Load Text File:
从指定路径加载文本文件,设定使用utf-8编码。
选项说明:
* path: txt文件路径。
输出:
string字符串。
## 使用示例:
![image](image/comfy_wordcloud_simple.png)
在workflow目录下有json格式的工作流示例文件。
## 安装方法:
* 在CompyUI插件目录(例如“CompyUI\custom_nodes\”)中打开cmd窗口,键入```git clone https://github.com/chflame163/ComfyUI_WordCloud.git```安装。或者下载解压zip文件,将得到的文件夹复制到 ComfyUI\custom_nodes\
* 安装依赖包,在资源管理器ComfyUI\custom_nodes\ComfyUI_WordCloud 插件目录位置打开cmd窗口,输入以下命令:
```..\..\..\python_embeded\python.exe -m pip install -r requirements.txt```
* 重新打开ComfyUI。
Binary file not shown.
Binary file not shown.
Binary file not shown.
+5 -2
View File
@@ -144,10 +144,12 @@ class ComfyWordCloud:
freq_dict = WordCloud().process_text(' '.join(jieba.cut(text)))
if not keynote_words == '':
keynote_list = list(re.split(r'[,,\s*]', keynote_words))
keynote_list = [x for x in keynote_list if x != ''] # 去除空字符
keynote_dict = {keynote_list[i]: keynote_weight + max(freq_dict.values()) for i in range(len(keynote_list))}
freq_dict.update(keynote_dict)
log(f"word frequencies dict generated, include {len(freq_dict)} words.")
font_path = font_dict[font_path]
if not os.path.exists(font_path):
font_path = os.path.join(os.path.join(os.path.dirname(os.path.dirname(os.path.normpath(__file__))), 'font'),
@@ -158,8 +160,9 @@ class ComfyWordCloud:
stopwords_set = set("")
if not stopwords == "":
# stopwords_set = set(STOPWORDS).union(set(re.split(r'[,,\s*]', stopwords))) # 与自带默认排除词集合合并
stopwords_set = set(re.split(r'[,,\s*]', stopwords))
stopwords_list = re.split(r'[,,\s*]', stopwords)
stopwords_set= set([x for x in stopwords_list if x != '']) # 去除空字符
# 同时在词典中删除(stopwords之bug)
for item in stopwords_set:
if item in freq_dict.keys():