How do I create/get/open the image from a URL request when the response looks like this:
\x00\x00\x00 ftypavif\x00\x00\x00\ ... \x87"y~\x13 $%\\\xad ... xb5\xa07tR\x80
Much longer, of course.
An example of such a URL is here (not my website).
Normally I do it like this:
import requests
import urllib, cStringIO, PIL, io
from PIL import Image
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
headers = {'User-agent': ua, 'Accept-Encoding':'gzip,deflate,sdch', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'}
# below url returns regular image and code works
imgurl = "https://media-cldnry.s-nbcnews.com/image/upload/t_focal-860x484,f_auto,q_auto:best/MSNBC/Components/Video/201610/tdy_food_brisket_161021__142565.jpg"
# below url does NOT work.
imgurl = "https://media-cldnry.s-nbcnews.com/image/upload/t_focal-860x484,f_avif,q_auto:eco,dpr_2/MSNBC/Components/Video/201610/tdy_food_brisket_161021__142565.jpg"
r = requests.get(imgurl, headers=None, verify=False,timeout=3)
image = Image.open(cStringIO.StringIO(r.content))
The error for the bad URL is "IOError: cannot identify image file ..."
I also tried
image = Image.open(io.BytesIO(r.content))
and various versions of
data = bytes.fromhex(r.content)
import binascii
data = binascii.a2b_hex(r.content)
They all return various errors.
Note that I am still using Python 2.7 with PIL (Pillow).
Turns out that the data format is AVIF. This isn't supported out of the box with PIL/PILLOW, at least not in Python 2.7.
I solved it by using pillow-avif-plugin.
After pip installing it, include
import pillow_avif
That's it. The old code then works:
image = Image.open(cStringIO.StringIO(r.content))