Prevent image save counter collisions
This commit is contained in:
+12
-1
@@ -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,6 +137,10 @@ 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}"
|
||||||
|
# Counter selection and the corresponding writes must be one critical
|
||||||
|
# section. Without this, overlapping ComfyUI executions can both see
|
||||||
|
# the same highest existing counter and overwrite one another.
|
||||||
|
with _SAVE_COUNTER_LOCK:
|
||||||
for index, image in enumerate(images):
|
for index, image in enumerate(images):
|
||||||
width, height = int(image.shape[2]), int(image.shape[1])
|
width, height = int(image.shape[2]), int(image.shape[1])
|
||||||
stem = str(pattern or "image_%counter%")
|
stem = str(pattern or "image_%counter%")
|
||||||
|
|||||||
@@ -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