Skip to content

Commit e2263cb

Browse files
authored
Handle image mode (#374)
* Handle image mode Change-Id: Idbd0d65f6359557adbf812190048514080d48e6f * format Change-Id: Ia8a386ef959907650dd4fa5b5ab7401e75b9484a
1 parent f08c789 commit e2263cb

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

Diff for: google/generativeai/types/content_types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
def pil_to_blob(img):
7474
bytesio = io.BytesIO()
75-
if isinstance(img, PIL.PngImagePlugin.PngImageFile):
75+
if isinstance(img, PIL.PngImagePlugin.PngImageFile) or img.mode == "RGBA":
7676
img.save(bytesio, format="PNG")
7777
mime_type = "image/png"
7878
else:

Diff for: tests/test_content.py

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import IPython.display
2525
import PIL.Image
2626

27+
import numpy as np
2728

2829
HERE = pathlib.Path(__file__).parent
2930
TEST_PNG_PATH = HERE / "test_img.png"
@@ -67,6 +68,7 @@ class ADataClassWithList:
6768
class UnitTests(parameterized.TestCase):
6869
@parameterized.named_parameters(
6970
["PIL", PIL.Image.open(TEST_PNG_PATH)],
71+
["RGBA", PIL.Image.fromarray(np.zeros([6, 6, 4], dtype=np.uint8))],
7072
["IPython", IPython.display.Image(filename=TEST_PNG_PATH)],
7173
)
7274
def test_png_to_blob(self, image):
@@ -77,6 +79,7 @@ def test_png_to_blob(self, image):
7779

7880
@parameterized.named_parameters(
7981
["PIL", PIL.Image.open(TEST_JPG_PATH)],
82+
["RGB", PIL.Image.fromarray(np.zeros([6, 6, 3], dtype=np.uint8))],
8083
["IPython", IPython.display.Image(filename=TEST_JPG_PATH)],
8184
)
8285
def test_jpg_to_blob(self, image):

0 commit comments

Comments
 (0)