google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.47k stars 289 forks source link

Automatic image blob creation doesn't handle P images with JPEG #511

Closed BluePigman closed 1 month ago

BluePigman commented 1 month ago

Description of the bug:

Currently P mode images cannot be uploaded to Gemini. https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes

Actual vs expected behavior:

import requests
from PIL import Image
import google.generativeai as genai

genai.configure(api_key)

media_url = "https://cdn.betterttv.net/emote/5f5a87416084af6c1719f9b4/3x"   

image = Image.open(requests.get(media_url, stream=True).raw)
response = genai.GenerativeModel("gemini-1.5-flash").generate_content(["Describe this image.", image])
description = response.text.replace('\n', ' ')
print(description)

Actual: Running this code gives the errors: KeyError: 'P' OSError: cannot write mode P as JPEG

Expected: If we add the following line before generating the response

image = image.convert("RGB")

The code runs with no errors.

Any other information you'd like to share?

This is similar to this issue solved previously: https://github.com/google-gemini/generative-ai-python/issues/160

manojssmk commented 1 month ago

I tried to run your code on Colab and faced the same issue Please find the gist here for reference. Thanks!

BluePigman commented 1 month ago

I tried to run your code on Colab and faced the same issue Please find the gist here for reference. Thanks!

You leaked your API key, just letting you know.

MarkDaoust commented 1 month ago

Ah, I see, thanks for reporting.

It looks like my first fix was junk. It looks like I can probably use img.get_format_mimetype() to resolve this for all supported mime-types.

MarkDaoust commented 1 month ago

@BluePigman @manojssmk please have a look at the fix in #523 and LMK if you have any feedback.

BluePigman commented 1 month ago

Looks good to me!

MarkDaoust commented 1 month ago

Fixed.