Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: imagen 3 inpainting and outpainting samples #13049

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Cloud Vertex AI sample for editing an image using a mask file with Imagen 3.
Inpainting can insert the object designated by the prompt into the masked
area.
"""
import os

from vertexai.preview import vision_models

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")


def edit_image_3_inpainting_insert_mask(
input_file: str,
mask_file: str,
output_file: str,
prompt: str,
) -> vision_models.ImageGenerationResponse:
# [START generativeaionvertexai_imagen_3_edit_image_inpainting_insert_mask]

import vertexai
from vertexai.preview.vision_models import Image, ImageGenerationModel, MaskReferenceImage, RawReferenceImage

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# input_file = "input-image.png"
# mask_file = "mask-image.png"
# output_file = "output-image.png"
# prompt = "" # The text prompt describing what you want to see inserted.

vertexai.init(project=PROJECT_ID, location="us-central1")

model = ImageGenerationModel.from_pretrained("imagen-3.0-capability-001")
base_img = Image.load_from_file(location=input_file)
mask_img = Image.load_from_file(location=mask_file)
base_ref_image = RawReferenceImage(image=base_img, reference_id=0)
mask_ref_image = MaskReferenceImage(
reference_id=1, image=mask_img, mask_mode="user_provided"
)

images = model.edit_image(
reference_images=[base_ref_image, mask_ref_image],
prompt=prompt,
edit_mode="inpainting-insert",
)

images[0].save(location=output_file, include_generation_parameters=False)

# Optional. View the edited image in a notebook.
# images[0].show()

print(f"Created output image using {len(images[0]._image_bytes)} bytes")
# Example response:
# Created output image using 1400814 bytes

# [END generativeaionvertexai_imagen_3_edit_image_inpainting_insert_mask]

return images


if __name__ == "__main__":
edit_image_3_inpainting_insert_mask(
input_file="../test_resources/woman.png",
mask_file="../test_resources/woman_inpainting_insert_mask.png",
output_file="../test_resources/woman_with_hat.png",
prompt="red hat",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Cloud Vertex AI sample for editing an image using a mask mode. The
mask mode is used to automatically select the background, foreground (i.e.,
the primary subject of the image), or an object based on segmentation class.
Inpainting can insert the object or background designated by the prompt.
"""
import os

from vertexai.preview import vision_models

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")


def edit_image_3_inpainting_insert_mask_mode(
input_file: str,
mask_mode: str,
output_file: str,
prompt: str,
) -> vision_models.ImageGenerationResponse:
# [START generativeaionvertexai_imagen_edit_image_3_inpainting_insert_mask_mode]

import vertexai
from vertexai.preview.vision_models import Image, ImageGenerationModel, MaskReferenceImage, RawReferenceImage

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# input_file = "input-image.png"
# mask_mode = "background" # 'background', 'foreground', or 'semantic'
# output_file = "output-image.png"
# prompt = "" # The text prompt describing what you want to see inserted.

vertexai.init(project=PROJECT_ID, location="us-central1")

model = ImageGenerationModel.from_pretrained("imagen-3.0-capability-001")
base_img = Image.load_from_file(location=input_file)
base_ref_image = RawReferenceImage(image=base_img, reference_id=0)
mask_ref_image = MaskReferenceImage(
reference_id=1, image=None, mask_mode=mask_mode
)

images = model.edit_image(
reference_images=[base_ref_image, mask_ref_image],
prompt=prompt,
edit_mode="inpainting-insert",
)

images[0].save(location=output_file, include_generation_parameters=False)

# Optional. View the edited image in a notebook.
# images[0].show()

print(f"Created output image using {len(images[0]._image_bytes)} bytes")
# Example response:
# Created output image using 1234567 bytes

# [END generativeaionvertexai_imagen_edit_image_3_inpainting_insert_mask_mode]

return images


if __name__ == "__main__":
edit_image_3_inpainting_insert_mask_mode(
input_file="../test_resources/woman.png",
mask_mode="background",
output_file="../test_resources/woman_at_beach.png",
prompt="beach",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import backoff

import edit_image_3_inpainting_insert_mask_mode

from google.api_core.exceptions import ResourceExhausted

_RESOURCES = os.path.join(os.path.dirname(__file__), "../test_resources")
_INPUT_FILE = os.path.join(_RESOURCES, "woman.png")
_MASK_MODE = "background"
_OUTPUT_FILE = os.path.join(_RESOURCES, "woman_at_beach.png")
_PROMPT = "beach"


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60)
def test_edit_image_3_inpainting_insert_mask_mode() -> None:
response = (
edit_image_3_inpainting_insert_mask_mode.edit_image_3_inpainting_insert_mask_mode(
_INPUT_FILE,
_MASK_MODE,
_OUTPUT_FILE,
_PROMPT,
)
)

assert len(response[0]._image_bytes) > 1000
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os

import backoff

import edit_image_3_inpainting_insert_mask

from google.api_core.exceptions import ResourceExhausted


_RESOURCES = os.path.join(os.path.dirname(__file__), "../test_resources")
_INPUT_FILE = os.path.join(_RESOURCES, "woman.png")
_MASK_FILE = os.path.join(_RESOURCES, "woman_inpainting_insert_mask.png")
_OUTPUT_FILE = os.path.join(_RESOURCES, "woman_with_hat.png")
_PROMPT = "hat"


@backoff.on_exception(backoff.expo, ResourceExhausted, max_time=60)
def test_edit_image_3_inpainting_insert_mask() -> None:
response = edit_image_3_inpainting_insert_mask.edit_image_3_inpainting_insert_mask(
_INPUT_FILE,
_MASK_FILE,
_OUTPUT_FILE,
_PROMPT,
)

assert len(response[0]._image_bytes) > 1000
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Cloud Vertex AI sample for editing an image using a mask file.
Inpainting can remove an object from the masked area.
"""
import os

from vertexai.preview import vision_models

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")


def edit_image_3_inpainting_remove_mask(
input_file: str,
mask_file: str,
output_file: str,
prompt: str,
) -> vision_models.ImageGenerationResponse:
# [START generativeaionvertexai_imagen_edit_image_3_inpainting_remove_mask]

import vertexai
from vertexai.preview.vision_models import Image, ImageGenerationModel, MaskReferenceImage, RawReferenceImage

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# input_file = "input-image.png"
# mask_file = "mask-image.png"
# output_file = "output-image.png"
# prompt = "" # The text prompt describing the entire image.

vertexai.init(project=PROJECT_ID, location="us-central1")

model = ImageGenerationModel.from_pretrained("imagen-3.0-capability-001")
base_img = Image.load_from_file(location=input_file)
mask_img = Image.load_from_file(location=mask_file)
base_ref_image = RawReferenceImage(image=base_img, reference_id=0)
mask_ref_image = MaskReferenceImage(
reference_id=1, image=mask_img, mask_mode="user_provided"
)

images = model.edit_image(
reference_images=[base_ref_image, mask_ref_image],
prompt=prompt,
edit_mode="inpainting-remove",
# Optional parameters
# negative_prompt="", # Describes the object being removed (i.e., "person")
)

images[0].save(location=output_file, include_generation_parameters=False)

# Optional. View the edited image in a notebook.
# images[0].show()

print(f"Created output image using {len(images[0]._image_bytes)} bytes")
# Example response:
# Created output image using 12345678 bytes

# [END generativeaionvertexai_imagen_edit_image_3_inpainting_remove_mask]

return images


if __name__ == "__main__":
edit_image_3_inpainting_remove_mask(
input_file="../test_resources/volleyball_game.png",
mask_file="../test_resources/volleyball_game_inpainting_remove_mask.png",
output_file="../test_resources/volleyball_game_single_blue_player.png",
prompt="volleyball game",
)
Loading