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://www.paleoeffect.com/wp-content/uploads/2011/06/800x414xpaleo_bread_wheat_recipe-800x414.jpg.pagespeed.ic.6pprrypoto.webp

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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -