Skip to content

Expand PIL image support. #523

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 6 commits into from
Aug 27, 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
25 changes: 24 additions & 1 deletion google/generativeai/types/content_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,34 @@


def pil_to_blob(img):
# When you load an image with PIL you get a subclass of PIL.Image
# The subclass knows what file type it was loaded from it has a `.format` class attribute
# and the `get_format_mimetype` method. Convert these back to the same file type.
#
# The base image class doesn't know its file type, it just knows its mode.
# RGBA converts to PNG easily, P[allet] converts to GIF, RGB to GIF.
# But for anything else I'm not going to bother mapping it out (for now) let's just convert to RGB and send it.
#
# References:
# - file formats: https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html
# - image modes: https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes

bytesio = io.BytesIO()
if isinstance(img, PIL.PngImagePlugin.PngImageFile) or img.mode == "RGBA":

get_mime = getattr(img, "get_format_mimetype", None)
if get_mime is not None:
# If the image is created from a file, convert back to the same file type.
img.save(bytesio, format=img.format)
mime_type = img.get_format_mimetype()
elif img.mode == "RGBA":
img.save(bytesio, format="PNG")
mime_type = "image/png"
elif img.mode == "P":
img.save(bytesio, format="GIF")
mime_type = "image/gif"
else:
if img.mode != "RGB":
img = img.convert("RGB")
img.save(bytesio, format="JPEG")
mime_type = "image/jpeg"
bytesio.seek(0)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
TEST_JPG_URL = "https://storage.googleapis.com/generativeai-downloads/data/test_img.jpg"
TEST_JPG_DATA = TEST_JPG_PATH.read_bytes()

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


# simple test function
def datetime():
Expand Down Expand Up @@ -88,6 +92,17 @@ def test_jpg_to_blob(self, image):
self.assertEqual(blob.mime_type, "image/jpeg")
self.assertStartsWith(blob.data, b"\xff\xd8\xff\xe0\x00\x10JFIF")

@parameterized.named_parameters(
["PIL", PIL.Image.open(TEST_GIF_PATH)],
["P", PIL.Image.fromarray(np.zeros([6, 6, 3], dtype=np.uint8)).convert("P")],
["IPython", IPython.display.Image(filename=TEST_GIF_PATH)],
)
def test_gif_to_blob(self, image):
blob = content_types.image_to_blob(image)
self.assertIsInstance(blob, protos.Blob)
self.assertEqual(blob.mime_type, "image/gif")
self.assertStartsWith(blob.data, b"GIF87a")

@parameterized.named_parameters(
["BlobDict", {"mime_type": "image/png", "data": TEST_PNG_DATA}],
["protos.Blob", protos.Blob(mime_type="image/png", data=TEST_PNG_DATA)],
Expand Down
Binary file added tests/test_img.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading