|
2 | 2 |
|
3 | 3 | import logging
|
4 | 4 | import os
|
| 5 | +from datetime import datetime, timezone |
5 | 6 | from typing import TYPE_CHECKING, Callable
|
6 | 7 |
|
| 8 | +from imaginairy.config import DEFAULT_SHARED_FILE_FORMAT_TEMPLATE, DEFAULT_SHARED_OUTDIR |
| 9 | + |
7 | 10 | if TYPE_CHECKING:
|
8 | 11 | from imaginairy.schema import ImaginePrompt
|
9 | 12 |
|
|
24 | 27 |
|
25 | 28 | def imagine_image_files(
|
26 | 29 | prompts: "list[ImaginePrompt] | ImaginePrompt",
|
27 |
| - outdir: str, |
| 30 | + outdir: str = DEFAULT_SHARED_OUTDIR, |
| 31 | + format_template: str = DEFAULT_SHARED_FILE_FORMAT_TEMPLATE, |
28 | 32 | precision: str = "autocast",
|
29 | 33 | record_step_images: bool = False,
|
30 |
| - output_file_extension: str = "jpg", |
31 | 34 | print_caption: bool = False,
|
32 | 35 | make_gif: bool = False,
|
33 | 36 | make_compare_gif: bool = False,
|
@@ -60,14 +63,18 @@ def imagine_image_files(
|
60 | 63 | from imaginairy.api.video_sample import generate_video
|
61 | 64 | from imaginairy.utils import get_next_filenumber, prompt_normalized
|
62 | 65 | from imaginairy.utils.animations import make_bounce_animation
|
| 66 | + from imaginairy.utils.format_file_name import format_filename |
63 | 67 | from imaginairy.utils.img_utils import pillow_fit_image_within
|
64 | 68 |
|
65 | 69 | generated_imgs_path = os.path.join(outdir, "generated")
|
66 | 70 | os.makedirs(generated_imgs_path, exist_ok=True)
|
67 | 71 |
|
68 | 72 | base_count = get_next_filenumber(generated_imgs_path)
|
69 |
| - output_file_extension = output_file_extension.lower() |
70 |
| - if output_file_extension not in {"jpg", "png"}: |
| 73 | + |
| 74 | + format_template, output_file_extension = os.path.splitext(format_template) |
| 75 | + if not output_file_extension: |
| 76 | + output_file_extension = ".jpg" |
| 77 | + elif output_file_extension not in {".jpg", ".png"}: |
71 | 78 | raise ValueError("Must output a png or jpg")
|
72 | 79 |
|
73 | 80 | if not isinstance(prompts, list):
|
@@ -101,15 +108,29 @@ def _record_step(img, description, image_count, step_count, prompt):
|
101 | 108 | if prompt.init_image:
|
102 | 109 | img_str = f"_img2img-{prompt.init_image_strength}"
|
103 | 110 |
|
104 |
| - basefilename = ( |
105 |
| - f"{base_count:06}_{prompt.seed}_{prompt.solver_type.replace('_', '')}{prompt.steps}_" |
106 |
| - f"PS{prompt.prompt_strength}{img_str}_{prompt_normalized(prompt.prompt_text)}" |
| 111 | + now = datetime.now(timezone.utc) |
| 112 | + |
| 113 | + format_data = { |
| 114 | + "file_sequence_number": f"{base_count:06}", |
| 115 | + "seed": f"{prompt.seed}", |
| 116 | + "steps": f"{prompt.steps}", |
| 117 | + "solver_type": f"{prompt.solver_type.replace('_', '')}", |
| 118 | + "prompt_strength": f"PS{prompt.prompt_strength}", |
| 119 | + "prompt_text": f"{prompt_normalized(prompt.prompt_text)}", |
| 120 | + "img_str": f"{img_str}", |
| 121 | + "file_extension": f"{output_file_extension}", |
| 122 | + "now": now, |
| 123 | + } |
| 124 | + |
| 125 | + basefilename = format_filename( |
| 126 | + format_template=format_template, data=format_data |
107 | 127 | )
|
| 128 | + |
108 | 129 | for image_type in result.images:
|
109 | 130 | subpath = os.path.join(outdir, image_type)
|
110 | 131 | os.makedirs(subpath, exist_ok=True)
|
111 | 132 | filepath = os.path.join(
|
112 |
| - subpath, f"{basefilename}_[{image_type}].{output_file_extension}" |
| 133 | + subpath, f"{basefilename}_[{image_type}]{output_file_extension}" |
113 | 134 | )
|
114 | 135 | result.save(filepath, image_type=image_type)
|
115 | 136 | logger.info(f" {image_type:<22} {filepath}")
|
|
0 commit comments