-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat: imagen 3 inpainting and outpainting samples #13049
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
51849d0
feat: imagen 3 inpainting and outpainting samples
katiemn 23ef69e
fix: spelling
katiemn 32cf486
fix: test failures
katiemn d6bec36
fix: file test path
katiemn fc2d739
feat: Imagen 3 product image samples
katiemn 8f01302
fix: lint
katiemn 5695719
Merge branch 'main' into imagen3-editing
katiemn 78e9912
fix: update prompt
katiemn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
generative_ai/image_generation/imagen3_editing/edit_image_3_inpainting_insert_mask.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
80 changes: 80 additions & 0 deletions
80
generative_ai/image_generation/imagen3_editing/edit_image_3_inpainting_insert_mask_mode.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
katiemn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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] | ||
katiemn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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", | ||
) |
41 changes: 41 additions & 0 deletions
41
...tive_ai/image_generation/imagen3_editing/edit_image_3_inpainting_insert_mask_mode_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
39 changes: 39 additions & 0 deletions
39
generative_ai/image_generation/imagen3_editing/edit_image_3_inpainting_insert_mask_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
81 changes: 81 additions & 0 deletions
81
generative_ai/image_generation/imagen3_editing/edit_image_3_inpainting_remove_mask.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.