Skip to content

Commit

Permalink
feat: 🎨 add support for image.size(0) == 0
Browse files Browse the repository at this point in the history
Attempt at ignoring a branch if the image size is 0.
  • Loading branch information
melMass committed Jul 14, 2023
1 parent c225da5 commit 629e2b5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 6 additions & 0 deletions nodes/image_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def do_interpolation(
film_model: interpolator.Interpolator,
):
n = images.size(0)
# check if images is an empty tensor and return it...
if n == 0:
return (images,)

# check if tensorflow GPU is available
available_gpus = tf.config.list_physical_devices("GPU")
if not len(available_gpus):
Expand Down Expand Up @@ -184,6 +188,8 @@ def export_prores(
fps: float,
prefix: str,
):
if images.size(0) == 0:
return ("",)
output_dir = Path(folder_paths.get_output_directory())
id = f"{prefix}_{uuid.uuid4()}.mov"

Expand Down
22 changes: 16 additions & 6 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys

from typing import Union, List
from .log import log


def add_path(path, prepend=False):
Expand Down Expand Up @@ -45,16 +46,21 @@ def add_path(path, prepend=False):
add_path((comfy_dir / "custom_nodes"))


def tensor2pil(image: torch.Tensor) -> Union[Image.Image, List[Image.Image]]:
def tensor2pil(image: torch.Tensor) -> List[Image.Image]:
batch_count = 1
if len(image.shape) > 3:
batch_count = image.size(0)

if batch_count == 1:
return Image.fromarray(
if batch_count > 1:
out = []
out.extend([tensor2pil(image[i]) for i in range(batch_count)])
return out

return [
Image.fromarray(
np.clip(255.0 * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)
)
return [tensor2pil(image[i]) for i in range(batch_count)]
]


def pil2tensor(image: Image.Image | List[Image.Image]) -> torch.Tensor:
Expand All @@ -76,5 +82,9 @@ def tensor2np(tensor: torch.Tensor) -> Union[np.ndarray, List[np.ndarray]]:
if len(tensor.shape) > 3:
batch_count = tensor.size(0)
if batch_count > 1:
return [tensor2np(tensor[i]) for i in range(batch_count)]
return np.clip(255.0 * tensor.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)
out = []
out.extend([tensor2np(tensor[i]) for i in range(batch_count)])
return out

return [np.clip(255.0 * tensor.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)]

0 comments on commit 629e2b5

Please sign in to comment.