Skip to content

Handle image mode #374

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

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion google/generativeai/types/content_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

def pil_to_blob(img):
bytesio = io.BytesIO()
if isinstance(img, PIL.PngImagePlugin.PngImageFile):
if isinstance(img, PIL.PngImagePlugin.PngImageFile) or img.mode == "RGBA":
img.save(bytesio, format="PNG")
mime_type = "image/png"
else:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import IPython.display
import PIL.Image

import numpy as np

HERE = pathlib.Path(__file__).parent
TEST_PNG_PATH = HERE / "test_img.png"
Expand Down Expand Up @@ -67,6 +68,7 @@ class ADataClassWithList:
class UnitTests(parameterized.TestCase):
@parameterized.named_parameters(
["PIL", PIL.Image.open(TEST_PNG_PATH)],
["RGBA", PIL.Image.fromarray(np.zeros([6, 6, 4], dtype=np.uint8))],
["IPython", IPython.display.Image(filename=TEST_PNG_PATH)],
)
def test_png_to_blob(self, image):
Expand All @@ -77,6 +79,7 @@ def test_png_to_blob(self, image):

@parameterized.named_parameters(
["PIL", PIL.Image.open(TEST_JPG_PATH)],
["RGB", PIL.Image.fromarray(np.zeros([6, 6, 3], dtype=np.uint8))],
["IPython", IPython.display.Image(filename=TEST_JPG_PATH)],
)
def test_jpg_to_blob(self, image):
Expand Down
Loading