Skip to content

Commit b8bbc75

Browse files
committed
fix(generate_image): improve input image handling and validate output filename extensions
1 parent 5efb732 commit b8bbc75

1 file changed

Lines changed: 36 additions & 29 deletions

File tree

skills/nano-banana-pro-openrouter/scripts/generate_image.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ def parse_args():
3838
help="Output resolution: 1K, 2K, or 4K.",
3939
)
4040
parser.add_argument(
41-
"--input-image",
42-
action="append",
43-
default=[],
44-
help=f"Optional input image path (repeatable, max {MAX_INPUT_IMAGES}).",
41+
"--input-image",
42+
action="append",
43+
default=[],
44+
help=f"Optional input image path (repeatable, max {MAX_INPUT_IMAGES}).",
4545
)
4646
return parser.parse_args()
4747

@@ -71,37 +71,44 @@ def build_message_content(prompt: str, input_images):
7171
content.append({"type": "image_url", "image_url": {"url": data_url}})
7272
return content
7373

74-
def parse_data_url(data_url: str):
75-
if not data_url.startswith("data:") or ";base64," not in data_url:
74+
def parse_data_url(data_url: str):
75+
if not data_url.startswith("data:") or ";base64," not in data_url:
7676
raise ValueError("Image URL is not a base64 data URL.")
77-
header, encoded = data_url.split(",", 1)
78-
mime = header[5:].split(";", 1)[0]
79-
try:
77+
header, encoded = data_url.split(",", 1)
78+
mime = header[5:].split(";", 1)[0]
79+
try:
8080
raw = base64.b64decode(encoded)
81-
except Exception as e:
81+
except Exception as e:
8282
raise SystemExit(f"Failed to decode base64 image payload: {e}")
83-
return mime, raw
83+
return mime, raw
8484

8585

8686
def resolve_output_paths(filename: str, image_count: int, mime: str):
87-
output_path = Path(filename)
88-
suffix = output_path.suffix
89-
if not suffix:
90-
suffix = MIME_TO_EXT.get(mime, ".png")
91-
output_path = output_path.with_suffix(suffix)
92-
93-
# Create parent directory if it doesn't exist (for absolute paths)
94-
if output_path.parent and str(output_path.parent) != '.':
95-
output_path.parent.mkdir(parents=True, exist_ok=True)
96-
97-
if image_count == 1:
98-
return [output_path]
99-
100-
paths = []
101-
for index in range(image_count):
102-
numbered = output_path.with_name(f"{output_path.stem}-{index + 1}{suffix}")
103-
paths.append(numbered)
104-
return paths
87+
output_path = Path(filename)
88+
suffix = output_path.suffix
89+
90+
# Validate/correct suffix matches MIME type
91+
expected_suffix = MIME_TO_EXT.get(mime, ".png")
92+
if suffix and suffix.lower() != expected_suffix.lower():
93+
print(f"Warning: filename extension '{suffix}' doesn't match returned MIME type '{mime}'. Using '{expected_suffix}' instead.")
94+
suffix = expected_suffix
95+
elif not suffix:
96+
suffix = expected_suffix
97+
98+
output_path = output_path.with_suffix(suffix)
99+
100+
# Create parent directory if it doesn't exist (for paths with parent directories, absolute or relative)
101+
if output_path.parent and str(output_path.parent) != '.':
102+
output_path.parent.mkdir(parents=True, exist_ok=True)
103+
104+
if image_count == 1:
105+
return [output_path]
106+
107+
paths = []
108+
for index in range(image_count):
109+
numbered = output_path.with_name(f"{output_path.stem}-{index + 1}{suffix}")
110+
paths.append(numbered)
111+
return paths
105112

106113

107114
def extract_image_url(image):

0 commit comments

Comments
 (0)