Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions comfy_extras/nodes_bounding_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from PIL import Image, ImageDraw, ImageEnhance, ImageFont
from typing_extensions import override

import nodes
from comfy_api.latest import ComfyExtension, io
from comfy_extras.color_util import hex_to_rgb, normalize_palette, readable_color

Expand Down Expand Up @@ -361,6 +362,9 @@ def execute(cls, width, height, editor_state=None, last_incoming=None, backgroun
ui = {"dims": [width, height]}
if incoming:
ui["input_bboxes"] = incoming
if background is not None and len(background) > 0:
saved = nodes.PreviewImage().save_images(background[:1], "comfy.bboxes.background")
ui["background_images"] = saved["ui"]["images"]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return io.NodeOutput(
preview,
fractions_to_bbox_frame(regions, width, height),
Expand Down
40 changes: 40 additions & 0 deletions tests-unit/comfy_extras_test/nodes_bounding_boxes_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os

import torch
from PIL import Image

import folder_paths
from comfy.cli_args import args as cli_args

if not torch.cuda.is_available():
cli_args.cpu = True

from comfy_extras.nodes_bounding_boxes import CreateBoundingBoxes # noqa: E402


def run(background=None):
return CreateBoundingBoxes.execute(width=64, height=64, background=background).ui


def test_echoes_the_first_background_image_as_a_temp_preview(tmp_path, monkeypatch):
monkeypatch.setattr(folder_paths, "get_temp_directory", lambda: str(tmp_path))
first = torch.zeros(1, 48, 80, 3)
second = torch.ones(1, 48, 80, 3)

ui = run(background=torch.cat([first, second]))

[entry] = ui["background_images"]
assert entry["type"] == "temp"
path = os.path.join(str(tmp_path), entry["subfolder"], entry["filename"])
with Image.open(path) as saved:
assert saved.size == (80, 48)
assert saved.getpixel((0, 0)) == (0, 0, 0)


def test_omits_the_background_preview_without_a_background(tmp_path, monkeypatch):
monkeypatch.setattr(folder_paths, "get_temp_directory", lambda: str(tmp_path))

ui = run()

assert "background_images" not in ui
assert ui["dims"] == [64, 64]
Loading