Skip to content

Commit 5e56b4f

Browse files
committed
fixes#511
Change-Id: I7303becbbd6a9441c406a28e84ecc86d2fdd35bd
1 parent 7342a62 commit 5e56b4f

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

google/generativeai/types/content_types.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,18 @@
7474

7575
def pil_to_blob(img):
7676
bytesio = io.BytesIO()
77-
if isinstance(img, PIL.PngImagePlugin.PngImageFile) or img.mode == "RGBA":
77+
78+
get_mime = getattr(img, "get_format_mimetype", None)
79+
if get_mime is not None:
80+
# If the image is created from a file, convert back to the same file type.
81+
img.save(bytesio, format=img.format)
82+
mime_type = img.get_format_mimetype()
83+
elif img.mode == "RGBA":
7884
img.save(bytesio, format="PNG")
7985
mime_type = "image/png"
8086
else:
87+
if img.mode != "RGB":
88+
img = img.convert('RGB')
8189
img.save(bytesio, format="JPEG")
8290
mime_type = "image/jpeg"
8391
bytesio.seek(0)

0 commit comments

Comments
 (0)