gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
怎么制作一个裁剪图片分辨率的comfy节点,分辨率裁剪为适合的64的倍数
- User
怎么制作一个裁剪图片分辨率的comfy节点,分辨率裁剪为适合的64的倍数
- Assistant
要在 ComfyUI 中创建一个可以将图片分辨率调整为 64 的倍数的节点,你需要使用 Python 编写一个自定义节点。下面是一个简单的例子,如何实现这个功能:
-
创建一个自定义节点的 Python 文件:
选择一个你运行 ComfyUI 的目录,然后在
custom_nodes文件夹下创建一个新的.py文件,比如resize_node.py。 -
编写节点代码:
在
resize_node.py文件中,添加以下代码:import cv2 import numpy as np from comfy.model import ModelRunContext, Input, Output, Node class ResizeToMultipleOf64(Node): def __init__(self): super().__init__("ResizeToMultipleOf64") self.add_input("image", Input.Image) self.add_output("resized_image", Output.Image) def execute(self, ctx: ModelRunContext): # 获取输入图像 image = ctx.get_input("image") # 获取图像的当前尺寸 height, width = image.shape[:2] # 计算新的尺寸 new_width = (width // 64) * 64 new_height = (height // 64) * 64 # 调整图像大小 resized_image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA) # 将调整后的图像作为输出 ctx.set_output("resized_image", resized_image) def get_node(): return ResizeToMultipleOf64()
-