Skip to content
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

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

Closed
BluePigman opened this issue Aug 19, 2024 · 6 comments
Closed

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

BluePigman opened this issue Aug 19, 2024 · 6 comments
Assignees
Labels
component:python sdk Issue/PR related to Python SDK type:feature request New feature request/enhancement

Comments

@BluePigman
Copy link

BluePigman commented Aug 19, 2024

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: #160

@manojssmk
Copy link

manojssmk commented Aug 20, 2024

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

@manojssmk manojssmk added component:python sdk Issue/PR related to Python SDK type:bug Something isn't working status:triaged Issue/PR triaged to the corresponding sub-team labels Aug 20, 2024
@BluePigman
Copy link
Author

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
Copy link
Collaborator

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 MarkDaoust added type:feature request New feature request/enhancement and removed type:bug Something isn't working labels Aug 24, 2024
@MarkDaoust
Copy link
Collaborator

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

@BluePigman
Copy link
Author

Looks good to me!

@MarkDaoust
Copy link
Collaborator

Fixed.

@github-actions github-actions bot removed the status:triaged Issue/PR triaged to the corresponding sub-team label Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:python sdk Issue/PR related to Python SDK type:feature request New feature request/enhancement
Projects
None yet
Development

No branches or pull requests

3 participants