0

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))
simon
  • 2,561
  • 16
  • 26
  • Try the solution [here](https://stackoverflow.com/questions/13137817/how-to-download-image-using-requests). If it works, let me know and we can close this as a duplicate. – juanpa.arrivillaga Aug 15 '17 at 18:49
  • None of these solutions work if I have the user-agent header. If I remove it they work. If I enter the address in chrome then it uses the exact same user-agent and does show the file in the browser. – simon Aug 16 '17 at 12:49

0 Answers0