Prevent image save counter collisions
This commit is contained in:
+36
-25
@@ -3,6 +3,7 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
import threading
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
@@ -15,6 +16,7 @@ import folder_paths
|
|||||||
|
|
||||||
_IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif", ".tif", ".tiff"}
|
_IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif", ".tif", ".tiff"}
|
||||||
_SERVE_TOKENS = OrderedDict()
|
_SERVE_TOKENS = OrderedDict()
|
||||||
|
_SAVE_COUNTER_LOCK = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
def resolve_serve_token(token):
|
def resolve_serve_token(token):
|
||||||
@@ -32,7 +34,10 @@ def _list_folder_image_files(real_folder, recursive=False):
|
|||||||
|
|
||||||
|
|
||||||
def _next_counter_for_relative_path(base_directory, relative_template):
|
def _next_counter_for_relative_path(base_directory, relative_template):
|
||||||
template = str(relative_template).replace("%counter%", "*")
|
relative_template = str(relative_template).replace("\\", "/")
|
||||||
|
if "%counter%" not in relative_template:
|
||||||
|
return 1
|
||||||
|
template = relative_template.replace("%counter%", "*")
|
||||||
parent = Path(base_directory) / Path(template).parent
|
parent = Path(base_directory) / Path(template).parent
|
||||||
stem = Path(template).name
|
stem = Path(template).name
|
||||||
if not parent.exists():
|
if not parent.exists():
|
||||||
@@ -40,6 +45,8 @@ def _next_counter_for_relative_path(base_directory, relative_template):
|
|||||||
pattern = re.compile(re.escape(stem).replace(r"\*", r"(\d+)") + r"(?:\.[^.]+)?$")
|
pattern = re.compile(re.escape(stem).replace(r"\*", r"(\d+)") + r"(?:\.[^.]+)?$")
|
||||||
highest = 0
|
highest = 0
|
||||||
for path in parent.iterdir():
|
for path in parent.iterdir():
|
||||||
|
if not path.is_file():
|
||||||
|
continue
|
||||||
match = pattern.match(path.name)
|
match = pattern.match(path.name)
|
||||||
if match:
|
if match:
|
||||||
highest = max(highest, int(match.group(1)))
|
highest = max(highest, int(match.group(1)))
|
||||||
@@ -130,30 +137,34 @@ class DumasSaveImageNode:
|
|||||||
base.mkdir(parents=True, exist_ok=True)
|
base.mkdir(parents=True, exist_ok=True)
|
||||||
ui_images = []
|
ui_images = []
|
||||||
extension = ".jpg" if format == "jpg" else f".{format}"
|
extension = ".jpg" if format == "jpg" else f".{format}"
|
||||||
for index, image in enumerate(images):
|
# Counter selection and the corresponding writes must be one critical
|
||||||
width, height = int(image.shape[2]), int(image.shape[1])
|
# section. Without this, overlapping ComfyUI executions can both see
|
||||||
stem = str(pattern or "image_%counter%")
|
# the same highest existing counter and overwrite one another.
|
||||||
stem = stem.replace("%input%", _safe_token(name)).replace("%input2%", _safe_token(name_2))
|
with _SAVE_COUNTER_LOCK:
|
||||||
stem = stem.replace("%width%", str(width)).replace("%height%", str(height)).replace("%batch_num%", str(index))
|
for index, image in enumerate(images):
|
||||||
relative = stem.replace("\\", "/") + extension
|
width, height = int(image.shape[2]), int(image.shape[1])
|
||||||
counter = _next_counter_for_relative_path(str(base), relative)
|
stem = str(pattern or "image_%counter%")
|
||||||
relative = relative.replace("%counter%", str(counter).zfill(3))
|
stem = stem.replace("%input%", _safe_token(name)).replace("%input2%", _safe_token(name_2))
|
||||||
path = base / Path(relative)
|
stem = stem.replace("%width%", str(width)).replace("%height%", str(height)).replace("%batch_num%", str(index))
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
relative = stem.replace("\\", "/") + extension
|
||||||
pil = _image_to_pil(image)
|
counter = _next_counter_for_relative_path(str(base), relative)
|
||||||
if format == "jpg":
|
relative = relative.replace("%counter%", str(counter).zfill(3))
|
||||||
pil.convert("RGB").save(path, "JPEG", quality=max(1, min(100, int(quality))))
|
path = base / Path(relative)
|
||||||
elif format == "webp":
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
pil.save(path, "WEBP", quality=max(1, min(100, int(quality))))
|
pil = _image_to_pil(image)
|
||||||
else:
|
if format == "jpg":
|
||||||
pil.save(path, "PNG", pnginfo=_build_pnginfo(prompt, extra_pnginfo) if embed_workflow else None)
|
pil.convert("RGB").save(path, "JPEG", quality=max(1, min(100, int(quality))))
|
||||||
if _is_within_directory(str(output_dir), str(path)):
|
elif format == "webp":
|
||||||
subfolder = str(path.parent.relative_to(output_dir)).replace(os.sep, "/")
|
pil.save(path, "WEBP", quality=max(1, min(100, int(quality))))
|
||||||
ui_images.append({"filename": path.name, "subfolder": "" if subfolder == "." else subfolder, "type": "output"})
|
else:
|
||||||
else:
|
pil.save(path, "PNG", pnginfo=_build_pnginfo(prompt, extra_pnginfo) if embed_workflow else None)
|
||||||
token = uuid.uuid4().hex
|
if _is_within_directory(str(output_dir), str(path)):
|
||||||
_SERVE_TOKENS[token] = str(path)
|
subfolder = str(path.parent.relative_to(output_dir)).replace(os.sep, "/")
|
||||||
ui_images.append({"filename": path.name, "subfolder": str(path.parent).replace(os.sep, "/"), "type": "external", "token": token})
|
ui_images.append({"filename": path.name, "subfolder": "" if subfolder == "." else subfolder, "type": "output"})
|
||||||
|
else:
|
||||||
|
token = uuid.uuid4().hex
|
||||||
|
_SERVE_TOKENS[token] = str(path)
|
||||||
|
ui_images.append({"filename": path.name, "subfolder": str(path.parent).replace(os.sep, "/"), "type": "external", "token": token})
|
||||||
return {"ui": {"images": ui_images}}
|
return {"ui": {"images": ui_images}}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ class FakeImage:
|
|||||||
|
|
||||||
def save(self, path, *args, **kwargs):
|
def save(self, path, *args, **kwargs):
|
||||||
self.saved_paths.append((path, args, kwargs))
|
self.saved_paths.append((path, args, kwargs))
|
||||||
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||||
|
open(path, "wb").close()
|
||||||
|
|
||||||
def convert(self, _mode):
|
def convert(self, _mode):
|
||||||
return self
|
return self
|
||||||
@@ -19,8 +21,23 @@ class FakeImage:
|
|||||||
class FakeBatch:
|
class FakeBatch:
|
||||||
shape = (1, 6, 8, 3)
|
shape = (1, 6, 8, 3)
|
||||||
|
|
||||||
|
def cpu(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def numpy(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def clip(self, _low, _high):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __mul__(self, _value):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def astype(self, _dtype):
|
||||||
|
return self
|
||||||
|
|
||||||
def __getitem__(self, _index):
|
def __getitem__(self, _index):
|
||||||
return types.SimpleNamespace(cpu=lambda: self, numpy=lambda: self, astype=lambda _dtype: self)
|
return self
|
||||||
|
|
||||||
|
|
||||||
class DumasImageNodeTests(unittest.TestCase):
|
class DumasImageNodeTests(unittest.TestCase):
|
||||||
@@ -67,6 +84,44 @@ class DumasImageNodeTests(unittest.TestCase):
|
|||||||
("image", "mask", "width", "height", "filename", "index", "total"),
|
("image", "mask", "width", "height", "filename", "index", "total"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_counter_uses_highest_existing_file(self):
|
||||||
|
with tempfile.TemporaryDirectory() as directory:
|
||||||
|
for name in ("image_001.png", "image_004.png", "image_002.png.jpg"):
|
||||||
|
open(os.path.join(directory, name), "wb").close()
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
self.nodes._next_counter_for_relative_path(directory, "image_%counter%.png"),
|
||||||
|
5,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_save_batch_assigns_distinct_counter_names(self):
|
||||||
|
with tempfile.TemporaryDirectory() as directory:
|
||||||
|
FakeImage.saved_paths.clear()
|
||||||
|
node = self.nodes.DumasSaveImageNode()
|
||||||
|
node.save_images(
|
||||||
|
[FakeBatch(), FakeBatch()],
|
||||||
|
directory,
|
||||||
|
"batch_%counter%",
|
||||||
|
"png",
|
||||||
|
95,
|
||||||
|
False,
|
||||||
|
True,
|
||||||
|
)
|
||||||
|
node.save_images(
|
||||||
|
[FakeBatch(), FakeBatch()],
|
||||||
|
directory,
|
||||||
|
"batch_%counter%",
|
||||||
|
"png",
|
||||||
|
95,
|
||||||
|
False,
|
||||||
|
True,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
[os.path.basename(path) for path, _args, _kwargs in FakeImage.saved_paths[-2:]],
|
||||||
|
["batch_003.png", "batch_004.png"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user