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\