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: Add Imagen edit_image support in Java SDK #500

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
116 changes: 114 additions & 2 deletions google/genai/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4605,7 +4605,7 @@ def generate_images(
self._api_client._verify_response(return_value)
return return_value

def edit_image(
def _edit_image(
self,
*,
model: str,
Expand Down Expand Up @@ -5558,6 +5558,62 @@ def generate_content_stream(
automatic_function_calling_history.append(func_call_content)
automatic_function_calling_history.append(func_response_content)

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 a text description and configuration.

Args:
model (str): The model to use.
prompt (str): A text description of the edit to apply to the image.
reference_images (list[Union[RawReferenceImage, MaskReferenceImage,
ControlReferenceImage, StyleReferenceImage, SubjectReferenceImage]): The
reference images for editing.
config (EditImageConfig): Configuration for editing.

Usage:

.. code-block:: python

from google.genai.types import RawReferenceImage, MaskReferenceImage

raw_ref_image = RawReferenceImage(
reference_id=1,
reference_image=types.Image.from_file(IMAGE_FILE_PATH),
)

mask_ref_image = MaskReferenceImage(
reference_id=2,
config=types.MaskReferenceConfig(
mask_mode='MASK_MODE_FOREGROUND',
mask_dilation=0.06,
),
)
response = client.models.edit_image(
model='imagen-3.0-capability-001',
prompt='man with dog',
reference_images=[raw_ref_image, mask_ref_image],
config=types.EditImageConfig(
edit_mode= "EDIT_MODE_INPAINT_INSERTION",
number_of_images= 1,
include_rai_reason= True,
)
)
response.generated_images[0].image.show()
# Shows a man with a dog instead of a cat.
"""
return self._edit_image(
model=model,
prompt=prompt,
reference_images=reference_images,
config=config,
)

def upscale_image(
self,
*,
Expand Down Expand Up @@ -5990,7 +6046,7 @@ async def generate_images(
self._api_client._verify_response(return_value)
return return_value

async def edit_image(
async def _edit_image(
self,
*,
model: str,
Expand Down Expand Up @@ -6923,6 +6979,62 @@ async def async_generator(model, contents, config):

return async_generator(model, contents, config)

async 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 a text description and configuration.

Args:
model (str): The model to use.
prompt (str): A text description of the edit to apply to the image.
reference_images (list[Union[RawReferenceImage, MaskReferenceImage,
ControlReferenceImage, StyleReferenceImage, SubjectReferenceImage]): The
reference images for editing.
config (EditImageConfig): Configuration for editing.

Usage:

.. code-block:: python

from google.genai.types import RawReferenceImage, MaskReferenceImage

raw_ref_image = RawReferenceImage(
reference_id=1,
reference_image=types.Image.from_file(IMAGE_FILE_PATH),
)

mask_ref_image = MaskReferenceImage(
reference_id=2,
config=types.MaskReferenceConfig(
mask_mode='MASK_MODE_FOREGROUND',
mask_dilation=0.06,
),
)
response = await client.aio.models.edit_image(
model='imagen-3.0-capability-001',
prompt='man with dog',
reference_images=[raw_ref_image, mask_ref_image],
config=types.EditImageConfig(
edit_mode= "EDIT_MODE_INPAINT_INSERTION",
number_of_images= 1,
include_rai_reason= True,
)
)
response.generated_images[0].image.show()
# Shows a man with a dog instead of a cat.
"""
return await self._edit_image(
model=model,
prompt=prompt,
reference_images=reference_images,
config=config,
)

async def list(
self,
*,
Expand Down