2013-06-18 11 views
5

Próbuję uzyskać obraz z następującego adresu URL:Python PIL: IOError: nie można zidentyfikować plik obrazu

image_url = http://www.eatwell101.com/wp-content/uploads/2012/11/Potato-Pancakes-recipe.jpg?b14316 

Kiedy przejdź do niej w przeglądarce, to na pewno wygląda jak obraz. Ale pojawia się błąd przy próbie:

import urllib, cStringIO, PIL 
from PIL import Image 

img_file = cStringIO.StringIO(urllib.urlopen(image_url).read()) 
image = Image.open(img_file) 

IOError: cannot identify image file

mam skopiowane setki obrazów w ten sposób, więc nie jestem pewien, co jest specjalnego tutaj. Czy mogę uzyskać ten obraz?

Odpowiedz

3

Problem nie występuje w obrazie.

>>> urllib.urlopen(image_url).read() 
'\n<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n<html>\n <head>\n <title>403 You are banned from this site. Please contact via a different client configuration if you believe that this is a mistake.</title>\n </head>\n <body>\n <h1>Error 403 You are banned from this site. Please contact via a different client configuration if you believe that this is a mistake.</h1>\n <p>You are banned from this site. Please contact via a different client configuration if you believe that this is a mistake.</p>\n <h3>Guru Meditation:</h3>\n <p>XID: 1806024796</p>\n <hr>\n <p>Varnish cache server</p>\n </body>\n</html>\n' 

Użycie user agent header rozwiąże problem.

opener = urllib2.build_opener() 
opener.addheaders = [('User-agent', 'Mozilla/5.0')] 
response = opener.open(image_url) 
img_file = cStringIO.StringIO(response.read()) 
image = Image.open(img_file) 
+0

Pracował jak urok. – user984003

+0

Ten heder był również wymagany dla niektórych innych obrazów: ("Zaakceptuj", "text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8 ') – user984003

4

podczas otwierania pliku przy użyciu

In [3]: f = urllib.urlopen('http://www.eatwell101.com/wp-content/uploads/2012/11/Potato-Pancakes-recipe.jpg') 

In [9]: f.code 
Out[9]: 403 

To nie jest powrót obrazu.

Możesz spróbować sprecyzować nagłówek agenta użytkownika, aby sprawdzić, czy możesz oszukać serwer, który myśli, że jesteś przeglądarką.

Korzystanie requests bibliotekę (bo łatwiej jest wysłać informacje nagłówka)

In [7]: f = requests.get('http://www.eatwell101.com/wp-content/uploads/2012/11/Potato-Pancakes-recipe.jpg', headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:16.0) Gecko/20100101 Firefox/16.0,gzip(gfe)'}) 
In [8]: f.status_code 
Out[8]: 200 
2

Aby uzyskać obraz, można najpierw zapisać obraz, a następnie załadować go do pliku PIL. na przykład:

import urllib2,PIL 

opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(), urllib2.HTTPCookieProcessor()) 
image_content = opener.open("http://www.eatwell101.com/wp-content/uploads/2012/11/Potato-Pancakes-recipe.jpg?b14316").read() 
opener.close() 

save_dir = r"/some/folder/to/save/image.jpg" 
f = open(save_dir,'wb') 
f.write(image_content) 
f.close() 

image = Image.open(save_dir) 
... 
+1

ok, dzięki, i zapomnij użyć "b", trybu binarnego – ghanbari

Powiązane problemy