Skip to content

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

@BluePigman

Description

@BluePigman

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions