lks-ai / anynode

A Node for ComfyUI that does what you ask it to do
MIT License
426 stars 27 forks source link

Error calling the generated function: name 'Image' is not defined #24

Open mythicalBOB opened 3 weeks ago

mythicalBOB commented 3 weeks ago

Persistent error when code imports Image from Pillow library for converting tensor to image ETC.

REFERENCE:

Here's the fixed version: Imports in code: ['from PIL import Image, ImageOps, ImageEnhance, ImageFilter', 'import numpy as np', 'import torch'] Stored script: def generated_function(input_data_1=None, input_data_2=None): def tensor_to_pil(tensor):

Convert tensor to PIL Image

    return Image.fromarray((tensor.squeeze(0).numpy() * 255).astype('uint8'))

def pil_to_tensor(image):
    # Convert PIL Image back to tensor
    return torch.tensor(np.array(image) / 255.0).unsqueeze(0).float()

def vibrant_color_soft_sketch(image):
    # Convert to grayscale
    gray = image.convert('L')

    # Invert image
    inverted = ImageOps.invert(gray)

    # Blur image to create the "sketch base"
    blurred = inverted.filter(ImageFilter.GaussianBlur(radius=10))

    # Merge gray and blurred images to create base sketch effect
    blended = Image.blend(gray, blurred, alpha=0.5)

    # Enhance color
    enhancer = ImageEnhance.Color(image)
    vibrant_image = enhancer.enhance(2)

    # Combine with sketch
    final_image = Image.blend(vibrant_image, blended.convert('RGB'), alpha=0.3)

    return final_image

if input_data_1 is not None and isinstance(input_data_1, torch.Tensor):
    # Convert tensor to PIL image
    pil_image = tensor_to_pil(input_data_1)

    # Apply vibrant color soft sketch effect
    sketch_image = vibrant_color_soft_sketch(pil_image)

    # Convert back to tensor
    output_tensor = pil_to_tensor(sketch_image)

    # Ensure the tensor is the same shape as the input
    return output_tensor.view(1, *input_data_1.shape[1:])

return None

Error calling the generated function: name 'Image' is not defined Traceback (most recent call last): File "/workspace/ComfyUI/custom_nodes/anynode/nodes/any.py", line 388, in go result = locals_dict[function_name](any, input_data_2=any2) File "", line 34, in generated_function_98fd3fc746254fc1bfa2f4405e37e79e File "", line 4, in tensor_to_pil NameError: name 'Image' is not defined !!! Exception during processing!!! name 'Image' is not defined Traceback (most recent call last): File "/workspace/ComfyUI/execution.py", line 151, in recursive_execute output_data, output_ui = get_output_data(obj, input_data_all) File "/workspace/ComfyUI/execution.py", line 81, in get_output_data return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True) File "/workspace/ComfyUI/execution.py", line 74, in map_node_over_list results.append(getattr(obj, func)(**slice_dict(input_data_all, i))) File "/workspace/ComfyUI/custom_nodes/anynode/nodes/any.py", line 395, in go raise e File "/workspace/ComfyUI/custom_nodes/anynode/nodes/any.py", line 388, in go result = locals_dict[function_name](any, input_data_2=any2) File "", line 34, in generated_function_98fd3fc746254fc1bfa2f4405e37e79e File "", line 4, in tensor_to_pil NameError: name 'Image' is not defined