olegchomp / TDComfyUI

TouchDesigner interface for ComfyUI
134 stars 9 forks source link

TOPs not encoded with alpha when converted to base64 to send to ComfyUI #10

Closed davideo4c closed 3 months ago

davideo4c commented 6 months ago

Hey there! Was having an issue sending the alpha channel from a TOP into ComfyUI's workflow. I made a small change to the imagetobytes function in the TdcomfyEXT script to achieve this.

    def imagetobytes(self, img):
        image = img.numpyArray(delayed=False)*255
        image = np.flipud(image).astype(np.uint8)
        # Check if the image has an alpha channel
        if image.shape[2] == 4:
            image = cv.cvtColor(image, cv.COLOR_BGRA2RGBA)
        else:
            image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
        imageData = cv.imencode('.png', image)[1].tobytes()
        init_image = str(base64.b64encode(imageData), "utf-8")
        return init_image   

This checks the numpyArray to see if it has an alpha channel, and if so makes sure to use BGRA2RGBA instead of the BGR2RGB conversion. Thanks a bunch for this!

olegchomp commented 3 months ago

Thank you! added in TDComfyUI-v.1.0.4