Skip to content

AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He

feat(image-generation): support Gemini reference-image editing

hugohe3e7f641f88432
diff --git a/skills/ppt-master/scripts/image_backends/backend_gemini.py b/skills/ppt-master/scripts/image_backends/backend_gemini.py
index bc3076a4..e4162009 100644
--- a/skills/ppt-master/scripts/image_backends/backend_gemini.py
+++ b/skills/ppt-master/scripts/image_backends/backend_gemini.py
@@ -2,7 +2,7 @@
"""
Gemini Image Generation Backend
-Generates images via the Google GenAI API (Gemini).
+Generates or edits images via the Google GenAI API (Gemini).
Used by image_gen.py as a backend module.
Configuration keys:
@@ -75,6 +75,19 @@ MINIMAL_THINKING_MODELS = {
"gemini-3.1-flash-image-preview",
}
+REFERENCE_IMAGE_MIME_TYPES = {
+ ".heic": "image/heic",
+ ".heif": "image/heif",
+ ".jpeg": "image/jpeg",
+ ".jpg": "image/jpeg",
+ ".png": "image/png",
+ ".webp": "image/webp",
+}
+
+# Signals to image_gen.py that this backend accepts a reference_image as
+# multimodal input to the same generate_content request used for generation.
+SUPPORTS_REFERENCE_IMAGE = True
+
def _model_id(model: str) -> str:
"""Return the final model path component for official capability checks."""
@@ -102,16 +115,44 @@ def _validate_model_options(model: str, aspect_ratio: str, image_size: str) -> N
)
+def _reference_image_mime_type(reference_image: str) -> str:
+ """Validate one local reference image and return its Gemini MIME type."""
+ image_path = Path(reference_image)
+ if not image_path.is_file():
+ raise FileNotFoundError(f"Reference image file not found: {reference_image}")
+
+ mime_type = REFERENCE_IMAGE_MIME_TYPES.get(image_path.suffix.lower())
+ if mime_type is None:
+ extensions = ", ".join(sorted(REFERENCE_IMAGE_MIME_TYPES))
+ raise ValueError(
+ f"Unsupported Gemini reference image format '{image_path.suffix or '<none>'}'. "
+ f"Supported extensions: {extensions}"
+ )
+ return mime_type
+
+
+def _reference_image_part(reference_image: str) -> types.Part:
+ """Load one supported local image as a Gemini inline-data part."""
+ image_path = Path(reference_image)
+ mime_type = _reference_image_mime_type(reference_image)
+
+ return types.Part.from_bytes(
+ data=image_path.read_bytes(),
+ mime_type=mime_type,
+ )
+
+
# ╔══════���═══════════════════════════════════════════════��═══════════╗
-# ║ Image Generation ║
+# ║ Image Generation and Editing ║
# ╚══════════════════════════════════════════════════════════════════╝
def _generate_image(api_key: str, prompt: str,
aspect_ratio: str = "1:1", image_size: str = "1K",
output_dir: str = None, filename: str = None,
- model: str = DEFAULT_MODEL, base_url: str = None) -> str:
+ model: str = DEFAULT_MODEL, base_url: str = None,
+ reference_image: str = None) -> str:
"""
- Image generation via Gemini API (streaming).
+ Image generation or editing via Gemini API (streaming).
Returns:
Path of the saved image file
@@ -137,18 +178,25 @@ def _generate_image(api_key: str, prompt: str,
)
config = types.GenerateContentConfig(**config_kwargs)
+ contents = [prompt]
+ if reference_image is not None:
+ contents.append(_reference_image_part(reference_image))
+
mode_label = "Proxy Mode" if base_url else "Official Mode"
print(f"[Gemini - {mode_label}]")
if base_url:
print(f" Base URL: {base_url}")
print(f" Model: {model}")
print(f" Prompt: {prompt[:120]}{'...' if len(prompt) > 120 else ''}")
+ if reference_image is not None:
+ print(f" Reference: {reference_image}")
print(f" Aspect Ratio: {aspect_ratio}")
print(f" Image Size: {image_size}")
print()
start_time = time.time()
- print(f" [..] Generating...", end="", flush=True)
+ operation = "Editing" if reference_image is not None else "Generating"
+ print(f" [..] {operation}...", end="", flush=True)
heartbeat_stop = threading.Event()
@@ -168,7 +216,7 @@ def _generate_image(api_key: str, prompt: str,
for chunk in client.models.generate_content_stream(
model=model,
- contents=[prompt],
+ contents=contents,
config=config,
):
elapsed = time.time() - start_time
@@ -213,9 +261,10 @@ def _generate_image(api_key: str, prompt: str,
def generate(prompt: str,
aspect_ratio: str = "1:1", image_size: str = "1K",
output_dir: str = None, filename: str = None,
- model: str = None, max_retries: int = MAX_RETRIES) -> str:
+ model: str = None, max_retries: int = MAX_RETRIES,
+ reference_image: str = None) -> str:
"""
- Gemini image generation with automatic retry.
+ Gemini image generation or image-to-image editing with automatic retry.
Reads credentials from the current process environment or a `.env` file:
GEMINI_API_KEY
@@ -223,13 +272,15 @@ def generate(prompt: str,
GEMINI_MODEL (optional override)
Args:
- prompt: Prompt text
+ prompt: Prompt text (edit instruction when reference_image is set)
aspect_ratio: Aspect ratio (e.g. "16:9", "1:1")
image_size: Image size ("512px", "1K", "2K", "4K", case-insensitive)
output_dir: Output directory
filename: Output filename (without extension)
model: Model name (default: gemini-3.1-flash-image)
max_retries: Maximum number of retries
+ reference_image: Optional source image path. When set, the image and
+ edit instruction are sent together as multimodal input.
Returns:
Path of the saved image file
@@ -255,12 +306,18 @@ def generate(prompt: str,
_validate_model_options(model, aspect_ratio, image_size)
+ if reference_image is not None:
+ # Validate local input before entering the retry loop. API failures can
+ # be transient; an unsupported or missing source image cannot be fixed
+ # by resending the same request.
+ _reference_image_mime_type(reference_image)
+
last_error = None
for attempt in range(max_retries + 1):
try:
return _generate_image(api_key, prompt,
aspect_ratio, image_size, output_dir,
- filename, model, base_url)
+ filename, model, base_url, reference_image)
except Exception as e:
last_error = e
if is_permanent_error(e):
diff --git a/skills/ppt-master/scripts/image_gen.py b/skills/ppt-master/scripts/image_gen.py
index 1d2e883a..854a478c 100644
--- a/skills/ppt-master/scripts/image_gen.py
+++ b/skills/ppt-master/scripts/image_gen.py
@@ -1269,8 +1269,8 @@ def main() -> None:
help=(
"Source image for image-to-image editing (single-image mode only). "
"When set, the prompt is used as the edit instruction. Only backends "
- "that support editing accept this (currently: openai). Not valid with "
- "--manifest / --render-md / --list-backends."
+ "that support editing accept this (currently: gemini, openai). Not "
+ "valid with --manifest / --render-md / --list-backends."
),
)
@@ -1398,7 +1398,8 @@ def main() -> None:
if not getattr(backend, "SUPPORTS_REFERENCE_IMAGE", False):
print(
f"Error: backend '{backend_name}' does not support image editing "
- "(--reference-image). Use a backend that does (currently: openai)."
+ "(--reference-image). Use a backend that does "
+ "(currently: gemini, openai)."
)
sys.exit(1)
gen_kwargs["reference_image"] = args.reference_image