-1

Can anyone help me with this problem? I have this URL, which holds an image of a motorcycle.

When I download it from request and then try to open it with Image.open(), I received the following error. The strange is that if I browse that file in my computer, I can see it perfectly:

UnidentifiedImageError                    Traceback (most recent call last)
Input In [241], in <cell line: 3>()
      1 from io import BytesIO
----> 3 Image.open('0.jpg')

File ~/opt/anaconda3/lib/python3.8/site-packages/PIL/Image.py:2967, in open(fp, mode, formats)
   2965 for message in accept_warnings:
   2966     warnings.warn(message)
-> 2967 raise UnidentifiedImageError(
   2968     "cannot identify image file %r" % (filename if filename else fp)
   2969 )

UnidentifiedImageError: cannot identify image file '0.jpg'

Here, I provide my code:

    def save_file(image, index):
        file = open(f"{index}.jpg", "wb")
        file.write(image)
        file.close()


def get_image(url):
    response = requests.get(f'{url}', headers=headers)
    image = response.content

    return image    
url = 'https://http2.mlstatic.com/D_NQ_NP_779386-MLA47939118380_102021-W.webp' 
image = get_image(url) #The response is 200, so it is OK
save_file(image,0)

Image.open('0.jpg') #It is in the same directory

EDIT: Here is part of the response.content ( It is the image itself)

b'RIFF\xc4i\x00\x00WEBPVP8 \xb8i\x00\x000\xe0\x01\x9d\x01*8\x02?\x01>m.\x93F$"\xa1\xa1,\x93\xdc\xf0\x80\r\x89eg\xe7\xc8N\x03\x95\xd7\xc5*=q\xf5rgx\xfe\xb3z\xef\xdc\xf0\xfd\xe3\x90\x7f\xe9\xce\x9f>Y=\x1d~\xe6z\xd4\xf9\xc7\xdfVc\x0c\xca\xa1\xae\xfb\x0c\xfcO\xf1\x7f\xe8}\xe4o\xef\xf0\x9e\x07}\xe0~\xbf\xac\x1f\xf0<\x1b\xf9\xdd\xff/\xa8\x8f\xe6\xbf\xd5\x7f\xe5\xfa]\xc6+\xae\x1f\xa7\xe8M\xef\xc7\xe3|\xfd\xe7\xa3\xf5}\x199\x8e\x7f3\xd47\xc9\xeb\xc1\xee\xa2\xe7\x99\x17\xbbW_\x04\xcf;P\x1d^K\xd6\x0e\xa5B=\xa4\xc1\xfe\xb9\x16;.\xa8\xc5\xf5X\
Nacho Eigu
  • 21
  • 4
  • always put code, data and full error message as text (not screenshot, not link) in question (not in comment). It will be more readable and easier to use in answer (simpler to select and copy), and more people will see it - so more people can help you. – furas Aug 18 '22 at 12:38
  • first check what you have in `response.content` - maybe you get HTML with warning for bots/scripts – furas Aug 18 '22 at 12:38
  • @furas Thank you, I adjusted the post. – Nacho Eigu Aug 18 '22 at 12:39
  • @furas The response is the image content, I will add it in the post. I wasnt blocked – Nacho Eigu Aug 18 '22 at 12:40
  • 1
    You download `.webp` but you write it with extension `.jpg` - PIL may has problem to recognize it. And I'm not sure if `PIL` can work with `.webp` – furas Aug 18 '22 at 12:40
  • your code works for me with Python 3.10, PIL 9.2.0 on Linux Mint 21. Maybe you have older PIL – furas Aug 18 '22 at 12:45
  • other idea: it may need to install some C/C++ library to work with format `.webp` - like in question for very old `PIL`: [python - Convert images to webP using Pillow - Stack Overflow](https://stackoverflow.com/questions/19860639/convert-images-to-webp-using-pillow) – furas Aug 18 '22 at 12:49

1 Answers1

0

Thank you @furia for your help. The problem was the incompatibility of the format.

Finally, I solved the issue with a modification in the URL. Instead of put .webp at the end, I replaced it with ".jpg" and so, I could download the image as JPG.

Nacho Eigu
  • 21
  • 4