2013-06-18 17 views
5

sto cercando di ottenere l'immagine dal seguente URL:Python PIL: IOError: non è in grado di identificare file immagine

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

Se navigando in un browser, sembra certo, come un'immagine. Ma ho un errore quando provo: centinaia

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

ho copiato di immagini in questo modo, quindi non sono sicuro di quello che è speciale qui. Posso ottenere questa immagine?

risposta

3

Il problema non esiste nell'immagine.

>>> 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' 

Utilizzando user agent header risolverà il problema.

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

Ha funzionato come un fascino. – user984003

+0

Questo heder è stato richiesto anche per alcune altre immagini: ('Accetta', 'text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8') – user984003

4

quando ho aperto il file utilizzando

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 

Questo non restituisce un'immagine.

Si potrebbe provare a specificare un'intestazione utente-agente per vedere se si può ingannare il server nel pensare di essere un browser.

Utilizzando requests biblioteca (perché è più facile per inviare le informazioni di intestazione)

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

Per ottenere un'immagine, è possibile prima salvare l'immagine e quindi caricarla su PIL. per esempio:

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, grazie, i dimentica l'uso 'b', modalità binaria – ghanbari

Problemi correlati