Skip to content
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
38 changes: 17 additions & 21 deletions google/genai/_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import inspect
import io
import logging
import pathlib
import re
import sys
import time
Expand Down Expand Up @@ -286,28 +287,23 @@ def t_caches_model(


def pil_to_blob(img: Any) -> types.Blob:
PngImagePlugin: Optional[builtin_types.ModuleType]
try:
import PIL.PngImagePlugin

PngImagePlugin = PIL.PngImagePlugin
except ImportError:
PngImagePlugin = None

bytesio = io.BytesIO()
if (
PngImagePlugin is not None
and isinstance(img, PngImagePlugin.PngImageFile)
or img.mode == 'RGBA'
):
img.save(bytesio, format='PNG')
def _blob_from_file() -> Optional[types.Blob]:
filepath = pathlib.Path(getattr(img, 'filename', ''))
if not filepath.is_file():
return None
# Return source bytes unchanged
image_bytes = filepath.read_bytes()
mime_type = img.get_format_mimetype()
return types.Blob(data=image_bytes, mime_type=mime_type)

def _blob_from_lossless_memory_image() -> types.Blob:
image_io = io.BytesIO()
img.save(image_io, format='PNG')
image_bytes = image_io.getvalue()
mime_type = 'image/png'
else:
img.save(bytesio, format='JPEG')
mime_type = 'image/jpeg'
bytesio.seek(0)
data = bytesio.read()
return types.Blob(mime_type=mime_type, data=data)
return types.Blob(data=image_bytes, mime_type=mime_type)

return _blob_from_file() or _blob_from_lossless_memory_image()


def t_function_response(
Expand Down