Add Image.open(url) tip from @mjpieters [ci skip]

This commit is contained in:
Alex Clark 2015-03-29 15:02:53 -04:00
parent 6151cfd411
commit 174d9ac083

View File

@ -12,3 +12,11 @@ This allows opening of files using both `urllib2` and `requests`, e.g.::
Image.open(urllib2.urlopen(url)) Image.open(urllib2.urlopen(url))
Image.open(requests.get(url, stream=True).raw) Image.open(requests.get(url, stream=True).raw)
If the response uses content-encoding (compression, either gzip or deflate) then this will fail as both the urllib2 and requests raw file object will produce compressed data in that case. Using Content-Encoding on images is rather non-sensical as most images are already compressed, but it can still happen.
For requests the work-around is to set the decode_content attribute on the raw object to True::
response = requests.get(url, stream=True)
response.raw.decode_content = True
image = Image.open(response.raw)