Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PIL: IOError: cannot identify image file

I am trying to get the image from the following URL:

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

When I navigate to it in a browser, it sure looks like an image. But I get an error when I try:

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

I have copied hundreds of images this way, so I'm not sure what's special here. Can I get this image?

like image 568
user984003 Avatar asked Jun 18 '13 20:06

user984003


3 Answers

when I open the file using

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

This is not returning an image.

You could try specifing a user-agent header to see if you can trick the server into thinking you are a browser.

Using requests library (because it is easier to send header information)

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
like image 120
dm03514 Avatar answered Nov 13 '22 19:11

dm03514


The problem exists not in the image.

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

Using user agent header will solve the 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)
like image 45
KostasT Avatar answered Nov 13 '22 19:11

KostasT


To get some image , you can first save the image and then , load it to PIL . for example:

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)
...
like image 2
Reza-S4 Avatar answered Nov 13 '22 20:11

Reza-S4