feat(plugin): add external image with base64 support, also with alpha image node

This commit is contained in:
BennyKok 2023-12-22 16:07:56 +08:00
parent d3b01ff64c
commit ccd3e43550
2 changed files with 65 additions and 3 deletions

View File

@ -34,12 +34,18 @@ class ComfyUIDeployExternalImage:
print("Fetching image from url: ", input_id)
response = requests.get(input_id)
image = Image.open(BytesIO(response.content))
elif input_id.startswith('data:image/png;base64,'):
import base64
from io import BytesIO
print("Decoding base64 image")
base64_image = input_id[input_id.find(",")+1:]
decoded_image = base64.b64decode(base64_image)
image = Image.open(BytesIO(decoded_image))
else:
raise ValueError("Invalid image url provided.")
# image_path = folder_paths.get_annotated_filepath(name)
# image = Image.open(image_path)
image = ImageOps.exif_transpose(image)
# image = image.convert("RGB")
image = image.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
return [image]

View File

@ -0,0 +1,56 @@
import folder_paths
from PIL import Image, ImageOps
import numpy as np
import torch
class ComfyUIDeployExternalImageAlpha:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input_id": (
"STRING",
{"multiline": False, "default": "input_image"},
),
},
"optional": {
"default_value": ("IMAGE",),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "run"
CATEGORY = "image"
def run(self, input_id, default_value=None):
image = default_value
try:
if input_id.startswith('http'):
import requests
from io import BytesIO
print("Fetching image from url: ", input_id)
response = requests.get(input_id)
image = Image.open(BytesIO(response.content))
elif input_id.startswith('data:image/png;base64,'):
import base64
from io import BytesIO
print("Decoding base64 image")
base64_image = input_id[input_id.find(",")+1:]
decoded_image = base64.b64decode(base64_image)
image = Image.open(BytesIO(decoded_image))
else:
raise ValueError("Invalid image url provided.")
image = ImageOps.exif_transpose(image)
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
return [image]
except:
return [image]
NODE_CLASS_MAPPINGS = {"ComfyUIDeployExternalImageAlpha": ComfyUIDeployExternalImageAlpha}
NODE_DISPLAY_NAME_MAPPINGS = {"ComfyUIDeployExternalImageAlpha": "External Image Alpha (ComfyUI Deploy)"}