From d09d2deaa30e16338257e6dad18f1cdc103ebb9b Mon Sep 17 00:00:00 2001 From: bai Date: Sun, 28 Dec 2025 14:07:22 +0800 Subject: [PATCH] =?UTF-8?q?```=20feat(image=5Fembeddings):=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0Image2Base64=E8=8A=82=E7=82=B9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增Image2Base64类,实现将图像张量转换为base64编码的功能。 该节点接收IMAGE和STRING类型的输入,输出按名称:编码内容格式的JSON字符串。 支持批量处理图像,并在NODE_CLASS_MAPPINGS和NODE_DISPLAY_NAME_MAPPINGS 中注册新节点,提供友好的显示名称。 ``` --- src/image_embeddings/nodes.py | 52 +++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/image_embeddings/nodes.py b/src/image_embeddings/nodes.py index 72acc16..1cebdb5 100644 --- a/src/image_embeddings/nodes.py +++ b/src/image_embeddings/nodes.py @@ -145,6 +145,52 @@ class ImageLoader: # 如果是URL,每次都重新加载,因为内容可能已更改 return image_path_or_url + +class Image2Base64: + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "images": ("IMAGE",), + "names": ("STRING",), }, + } + CATEGORY = "utils" + RETURN_TYPES = ('STRING',) + FUNCTION = "image_to_base64" + def image_to_base64(self, images,names): + """ + 将输入的图像张量转换为base64编码,并按名称:编码内容的格式输出JSON字符串 + """ + + # 创建结果字典 + result_dict = {} + file_names=[] + import base64 + if isinstance(names, list): + file_names = names + else: + file_names = [names] + # 遍历图像批次 + for index, image in enumerate(images): + + # 转换图像张量为PIL图像 + n = 255. * image.cpu().numpy() + img = Image.fromarray(np.clip(n, 0, 255).astype(np.uint8)) + + # 将PIL图像转换为base64 + buffer = BytesIO() + img.save(buffer, format="PNG") + img_bytes = buffer.getvalue() + img_base64 = base64.b64encode(img_bytes).decode('utf-8') + + # 添加到结果字典 + result_dict[file_names[index]] = img_base64 + + # 将结果字典转换为JSON字符串 + json_string = json.dumps(result_dict) + + return (json_string,) + class VisionOutputEmbedding2JSON: @classmethod def INPUT_TYPES(s): @@ -237,12 +283,14 @@ class ImageHashNode: NODE_CLASS_MAPPINGS = { "ImageLoader": ImageLoader, "OutputEmbedding": VisionOutputEmbedding2JSON, - "ImageHash": ImageHashNode + "ImageHash": ImageHashNode, + "Image2Base64": Image2Base64, } # A dictionary that contains the friendly/humanly readable titles for the nodes NODE_DISPLAY_NAME_MAPPINGS = { "ImageLoader": "Image Loader (Path or URL)", "OutputEmbedding": "Output Embedding to JSON", - "ImageHash": "Image Hash" + "ImageHash": "Image Hash", + "Image2Base64": "Image to Base64", } \ No newline at end of file