I can open this jpg link in my browser and it displays fine. But when I try and read it in python using the exact same headers it is unreadable.
PIL says "OSError: cannot identify image file <_io.BytesIO object at 0x00000145034C9EB8>"; and the file written to disk cannot be opened.
EDIT: I discovered if I remove the user-agent header then it works.....but strangely if I enter the address in chrome then it uses the exact same user-agent and does show the file in the browser.
import requests
from PIL import Image
import io
headers = {"Host": "i.telegraph.co.uk",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36",
"Upgrade-Insecure-Requests": "1",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en;q=0.8"}
link="http://i.telegraph.co.uk/multimedia/archive/02068/police-news_2068686b.jpg"
r=requests.get(link, headers=headers)
print(r.status_code)
print(r.headers)
print(len(r.content))
with open("c:/users/s/documents/temp123.jpg", "wb") as f:
f.write(r.content)
i=Image.open(io.BytesIO(r.content))