Skip to content

Commit 16c10e7

Browse files
committed
add gif support
Change-Id: Ibc8e091c63d30626f78510156e8c024014dddcca
1 parent 5e56b4f commit 16c10e7

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

google/generativeai/types/content_types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def pil_to_blob(img):
8383
elif img.mode == "RGBA":
8484
img.save(bytesio, format="PNG")
8585
mime_type = "image/png"
86+
elif img.mode == "P":
87+
img.save(bytesio, format="GIF")
88+
mime_type = "image/gif"
8689
else:
8790
if img.mode != "RGB":
8891
img = img.convert('RGB')

tests/test_content.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
TEST_JPG_URL = "https://storage.googleapis.com/generativeai-downloads/data/test_img.jpg"
3636
TEST_JPG_DATA = TEST_JPG_PATH.read_bytes()
3737

38+
TEST_GIF_PATH = HERE / "test_img.gif"
39+
TEST_GIF_URL = "https://storage.googleapis.com/generativeai-downloads/data/test_img.gif"
40+
TEST_GIF_DATA = TEST_GIF_PATH.read_bytes()
3841

3942
# simple test function
4043
def datetime():
@@ -88,6 +91,17 @@ def test_jpg_to_blob(self, image):
8891
self.assertEqual(blob.mime_type, "image/jpeg")
8992
self.assertStartsWith(blob.data, b"\xff\xd8\xff\xe0\x00\x10JFIF")
9093

94+
@parameterized.named_parameters(
95+
["PIL", PIL.Image.open(TEST_GIF_PATH)],
96+
["P", PIL.Image.fromarray(np.zeros([6, 6, 3], dtype=np.uint8)).convert('P')],
97+
["IPython", IPython.display.Image(filename=TEST_GIF_PATH)],
98+
)
99+
def test_gif_to_blob(self, image):
100+
blob = content_types.image_to_blob(image)
101+
self.assertIsInstance(blob, protos.Blob)
102+
self.assertEqual(blob.mime_type, "image/gif")
103+
self.assertStartsWith(blob.data, b"GIF87a")
104+
91105
@parameterized.named_parameters(
92106
["BlobDict", {"mime_type": "image/png", "data": TEST_PNG_DATA}],
93107
["protos.Blob", protos.Blob(mime_type="image/png", data=TEST_PNG_DATA)],

0 commit comments

Comments
 (0)