python - Get image from image url: IOError: cannot identify image file -
i using python requests image file image url.
the below code works in cases, starting fail more , more urls.
import requests image_url = "<url_here>" headers = {'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/29.0.1547.76 safari/537.36', 'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','accept-encoding':'gzip,deflate,sdch'} r = requests.get(image_url, headers=headers) image = image.open(cstringio.stringio(r.content))
if gives error try different header (this solved issues in past):
headers = {'user-agent': 'mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/29.0.1547.76 safari/537.36', 'accept':'image/webp,*/*;q=0.8','accept-encoding':'gzip,deflate,sdch'}
however, these urls (among others) don't work. give "ioerror: cannot identify image file" error.
http://cdn.casaveneracion.com/vegetarian/2013/08/vegan-spaghetti1.jpg
http://www.rachaelray.com/site/images/sidebar-heading-more-recipes-2.svg
it shows images fine in browser using urls. don't know if have same issue.
you using python imaging library (pil) provide image class mentioned in last line of code.
- the paleo effect image webp file. webp isn't supported format pil.
- the casa veneracion url not link image file - returns 302 redirect html file. (see yourself.)
- the rachael ray image svg file. svg isn't supported format pil.
see bottom of this documentation image formats supported pil.
Comments
Post a Comment