Skip to content

Commit 67611ed

Browse files
MarkDaoustcopybara-github
authored andcommitted
fix: Return genai.types.Image, not PIL.Image
PiperOrigin-RevId: 801081914
1 parent 214d017 commit 67611ed

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

google/genai/tests/transformers/test_blobs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_image(image_jpeg):
4444
assert blob.data[6:10] == b'JFIF'
4545
assert blob.mime_type == 'image/jpeg'
4646

47-
round_trip_image = blob.as_image()
47+
round_trip_image = blob.as_image()._pil_image
4848
assert round_trip_image is not None
4949
assert round_trip_image.size == image_jpeg.size
5050
assert round_trip_image.mode == image_jpeg.mode
@@ -59,7 +59,7 @@ def test_part_image(image_jpeg):
5959
assert part.inline_data.data[6:10] == b'JFIF'
6060
assert part.inline_data.mime_type == 'image/jpeg'
6161

62-
round_trip_image = part.as_image()
62+
round_trip_image = part.as_image()._pil_image
6363
assert round_trip_image is not None
6464
assert round_trip_image.size == image_jpeg.size
6565
assert round_trip_image.mode == image_jpeg.mode

google/genai/types.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import datetime
2020
from enum import Enum, EnumMeta
2121
import inspect
22-
import io
2322
import json
2423
import logging
2524
import sys
@@ -824,20 +823,18 @@ class Blob(_common.BaseModel):
824823
description="""Required. The IANA standard MIME type of the source data.""",
825824
)
826825

827-
def as_image(self) -> Optional['PIL_Image']:
828-
"""Returns the Blob as a PIL Image, or None if the Blob is not an image."""
826+
def as_image(self) -> Optional['Image']:
827+
"""Returns the Blob as a Image, or None if the Blob is not an image."""
829828
if (
830829
not self.data
831830
or not self.mime_type
832831
or not self.mime_type.startswith('image/')
833832
):
834833
return None
835-
if not _is_pillow_image_imported:
836-
raise ImportError(
837-
'The PIL module is not available. Please install the Pillow'
838-
' package. `pip install pillow`'
839-
)
840-
return PIL.Image.open(io.BytesIO(self.data))
834+
return Image(
835+
image_bytes=self.data,
836+
mime_type=self.mime_type,
837+
)
841838

842839

843840
class BlobDict(TypedDict, total=False):
@@ -1098,7 +1095,7 @@ class Part(_common.BaseModel):
10981095
default=None, description="""Optional. Text part (can be code)."""
10991096
)
11001097

1101-
def as_image(self) -> Optional['PIL_Image']:
1098+
def as_image(self) -> Optional['Image']:
11021099
"""Returns the part as a PIL Image, or None if the part is not an image."""
11031100
if not self.inline_data:
11041101
return None
@@ -6222,13 +6219,19 @@ def show(self) -> None:
62226219

62236220
This method only works in a notebook environment.
62246221
"""
6225-
try:
6226-
from IPython import display as IPython_display
6227-
except ImportError:
6228-
IPython_display = None
6222+
in_notebook = 'ipykernel' in sys.modules
6223+
if in_notebook:
6224+
try:
6225+
from IPython import display as IPython_display
6226+
except ImportError:
6227+
IPython_display = None
62296228

6230-
if IPython_display:
6231-
IPython_display.display(self._pil_image)
6229+
if IPython_display:
6230+
IPython_display.display(self._pil_image)
6231+
else:
6232+
img = self._pil_image
6233+
if img is not None:
6234+
img.show()
62326235

62336236
@property
62346237
def _pil_image(self) -> Optional['PIL_Image']:

0 commit comments

Comments
 (0)