Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Googleapis Python genai Models Edit Image

From Leeroopedia
Revision as of 12:49, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Googleapis_Python_genai_Models_Edit_Image.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Computer_Vision, Image_Processing
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for editing existing images using text instructions and reference images provided by the google-genai models module.

Description

Models.edit_image applies text-guided edits to images. It accepts a model identifier, an edit prompt, and a list of reference images (RawReferenceImage for the source image, MaskReferenceImage for optional masks). Optional configuration controls the edit mode, number of output images, and output format. The method returns an EditImageResponse with the edited results.

Usage

Call client.models.edit_image with the edit model, a text prompt describing the edit, and reference images. The reference_images list should contain at least one RawReferenceImage (the source). Add a MaskReferenceImage for targeted region editing.

Code Reference

Source Location

Signature

class Models:
    def edit_image(
        self,
        *,
        model: str,
        prompt: str,
        reference_images: list[types._ReferenceImageAPIOrDict],
        config: Optional[types.EditImageConfigOrDict] = None,
    ) -> types.EditImageResponse:
        """Edits an image based on text instructions and reference images.

        Args:
            model: Edit model ID.
            prompt: Text description of the desired edit.
            reference_images: List of RawReferenceImage and/or MaskReferenceImage.
            config: Optional edit configuration.
        """

Import

from google import genai
from google.genai import types

I/O Contract

Inputs

Name Type Required Description
model str Yes Edit model ID
prompt str Yes Text description of the edit
reference_images list[_ReferenceImageAPIOrDict] Yes List of RawReferenceImage and/or MaskReferenceImage
config.edit_mode Optional[EditMode] No Edit mode (inpainting, outpainting, etc.)
config.number_of_images Optional[int] No Number of edited images to generate

Outputs

Name Type Description
EditImageResponse EditImageResponse Response with edited image results

Usage Examples

Edit with Raw Reference

from google import genai
from google.genai import types

client = genai.Client(api_key="YOUR_API_KEY")

# Load source image
source_image = types.RawReferenceImage(
    reference_image=types.Image.from_file(location="source.jpg"),
    reference_id=0,
    reference_type="REFERENCE_TYPE_RAW",
)

response = client.models.edit_image(
    model="imagen-3.0-capability-001",
    prompt="Change the background to a beach sunset",
    reference_images=[source_image],
    config=types.EditImageConfig(
        number_of_images=1,
    ),
)

response.generated_images[0].image.save("edited.png")

Edit with Mask

source = types.RawReferenceImage(
    reference_image=types.Image.from_file(location="photo.jpg"),
    reference_id=0,
    reference_type="REFERENCE_TYPE_RAW",
)

mask = types.MaskReferenceImage(
    reference_image=types.Image.from_file(location="mask.png"),
    reference_id=1,
    reference_type="REFERENCE_TYPE_MASK",
    mask_mode="MASK_MODE_USER_PROVIDED",
    mask_dilation=0.01,
)

response = client.models.edit_image(
    model="imagen-3.0-capability-001",
    prompt="Replace the masked area with a garden",
    reference_images=[source, mask],
)

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment