Add option to override saved images
This commit is contained in:
parent
8e5e09f7d8
commit
d80b6e520f
@ -44,10 +44,15 @@ class GeneratedImage:
|
|||||||
self.init_image_path = init_image_path
|
self.init_image_path = init_image_path
|
||||||
self.init_image_strength = init_image_strength
|
self.init_image_strength = init_image_strength
|
||||||
|
|
||||||
def save(self, path: t.Union[str, pathlib.Path], export_json_metadata: bool = False) -> None:
|
def save(
|
||||||
|
self,
|
||||||
|
path: t.Union[str, pathlib.Path],
|
||||||
|
export_json_metadata: bool = False,
|
||||||
|
overwrite: bool = False,
|
||||||
|
) -> None:
|
||||||
from mflux import ImageUtil
|
from mflux import ImageUtil
|
||||||
|
|
||||||
ImageUtil.save_image(self.image, path, self._get_metadata(), export_json_metadata)
|
ImageUtil.save_image(self.image, path, self._get_metadata(), export_json_metadata, overwrite)
|
||||||
|
|
||||||
def _get_metadata(self) -> dict:
|
def _get_metadata(self) -> dict:
|
||||||
"""Generate metadata for reference as well as input data for
|
"""Generate metadata for reference as well as input data for
|
||||||
|
|||||||
@ -114,22 +114,24 @@ class ImageUtil:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def save_image(
|
def save_image(
|
||||||
image: PIL.Image.Image,
|
image: PIL.Image.Image,
|
||||||
path: t.Union[str, pathlib.Path],
|
path: t.Union[str, pathlib.Path],
|
||||||
metadata: dict | None = None,
|
metadata: dict | None = None,
|
||||||
export_json_metadata: bool = False
|
export_json_metadata: bool = False,
|
||||||
|
overwrite: bool = False
|
||||||
) -> None: # fmt: off
|
) -> None: # fmt: off
|
||||||
file_path = pathlib.Path(path)
|
file_path = pathlib.Path(path)
|
||||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
file_name = file_path.stem
|
file_name = file_path.stem
|
||||||
file_extension = file_path.suffix
|
file_extension = file_path.suffix
|
||||||
|
|
||||||
# If a file already exists, create a new name with a counter
|
# If a file already exists and overwrite is False, create a new name with a counter
|
||||||
counter = 1
|
if not overwrite:
|
||||||
while file_path.exists():
|
counter = 1
|
||||||
new_name = f"{file_name}_{counter}{file_extension}"
|
while file_path.exists():
|
||||||
file_path = file_path.with_name(new_name)
|
new_name = f"{file_name}_{counter}{file_extension}"
|
||||||
counter += 1
|
file_path = file_path.with_name(new_name)
|
||||||
|
counter += 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Save image without metadata first
|
# Save image without metadata first
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user