Gaurish Ninnekar
Gaurish Ninnekar

Reputation: 21

facing issue with PIL package while trying to prompt using image with google gemini-pro-vision and python

I was facing an issue while using python PIL package with gemini-pro-vision, following the google documentation.

Below is the code. Where I was trying to generate content using image via PIL package.

import PIL.Image
img = PIL.Image.open('cat.jpg')

model = genai.GenerativeModel('gemini-pro-vision')
response = model.generate_content(img.any())

print(response.text)

This is the error, I was getting. And same issue I was facing with opencv library as well.

TypeError                                 Traceback (most recent call last)
Cell In[47], line 2
      1 model = genai.GenerativeModel('gemini-pro-vision')
----> 2 response = model.generate_content(img)
      3 print(response.text)

File ~/workspace/Google_gemini_api/.venv/lib/python3.11/site-packages/google/generativeai/generative_models.py:234, in GenerativeModel.generate_content(self, contents, generation_config, safety_settings, stream, **kwargs)
    224 @string_utils.set_doc(_GENERATE_CONTENT_DOC)
    225 def generate_content(
    226     self,
   (...)
    232     **kwargs,
    233 ) -> generation_types.GenerateContentResponse:
--> 234     request = self._prepare_request(
    235         contents=contents,
    236         generation_config=generation_config,
    237         safety_settings=safety_settings,
    238         **kwargs,
    239     )
    240     if self._client is None:
    241         self._client = client.get_default_generative_client()

File ~/workspace/Google_gemini_api/.venv/lib/python3.11/site-packages/google/generativeai/generative_models.py:204, in GenerativeModel._prepare_request(self, contents, generation_config, safety_settings, **kwargs)
    201 if not contents:
...
    146 )

TypeError: Could not create `Blob`, expected `Blob`, `dict` or an `Image` type(`PIL.Image.Image` or `IPython.display.Image`).
Got a: <class 'PIL.JpegImagePlugin.JpegImageFile'>
Value: <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=276x183 at 0x7F603CB3ED90>

Upvotes: 0

Views: 1043

Answers (1)

Gaurish Ninnekar
Gaurish Ninnekar

Reputation: 21

You could use,

from IPython.display import Image
from IPython.core.display import HTML

img = Image('cat.jpg')

it gets the work done!!

Upvotes: 0

Related Questions