Implementation:Googleapis Python genai Image Show Save
| Knowledge Sources | |
|---|---|
| Domains | Computer_Vision, Data_Extraction |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for displaying, saving, and manipulating generated images provided by the google-genai types module.
Description
The Image class provides methods for handling image output: show() displays the image in a Jupyter notebook using IPython or PIL, save(location) writes the image to a local file, from_file(location) loads an image from disk. The class stores image data as image_bytes and/or gcs_uri, with mime_type indicating the format. The internal _pil_image property provides access to a PIL Image object for advanced manipulation.
Usage
Access images from response objects (e.g., response.generated_images[0].image). Call .show() in notebooks for visual inspection. Call .save("path.png") to persist. Use Image.from_file("path.png") to load images for use as inputs to edit or upscale operations.
Code Reference
Source Location
- Repository: googleapis/python-genai
- File: google/genai/types.py
- Lines: L7585-7700
Signature
class Image(_common.BaseModel):
"""An image."""
gcs_uri: Optional[str] = None
image_bytes: Optional[bytes] = None
mime_type: Optional[str] = None
@classmethod
def from_file(
cls, *, location: str, mime_type: Optional[str] = None
) -> 'Image':
"""Loads an image from a local file."""
def show(self) -> None:
"""Displays the image in a notebook environment."""
def save(self, location: str) -> None:
"""Saves the image to a local file.
Args:
location: Local file path to save to.
"""
Import
from google.genai import types
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| location (from_file) | str | Yes | Local file path to load |
| location (save) | str | Yes | Local file path to save to |
| mime_type | Optional[str] | No | MIME type (auto-detected if not specified) |
Outputs
| Name | Type | Description |
|---|---|---|
| Image (from_file) | Image | Loaded Image object |
| show() | None | Displays image in notebook (side effect) |
| save() | None | Writes image to file (side effect) |
| _pil_image | PIL.Image.Image | PIL Image for advanced manipulation |
Usage Examples
Display and Save Generated Images
from google import genai
from google.genai import types
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_images(
model="imagen-4.0-generate-001",
prompt="A cute robot painting a landscape"
)
for i, gen_image in enumerate(response.generated_images):
# Display in notebook
gen_image.image.show()
# Save to file
gen_image.image.save(f"robot_painting_{i}.png")
# Access raw bytes
raw_bytes = gen_image.image.image_bytes
print(f"Image {i}: {len(raw_bytes)} bytes")
Load Image from File
from google.genai import types
# Load an image for use as input
image = types.Image.from_file(location="my_photo.jpg")
# Use in edit or upscale operations
upscale_response = client.models.upscale_image(
model="imagen-3.0-generate-001",
image=image,
upscale_factor="x2"
)