You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Opening this in response to this report on matplotlib. Attempting to open remote image files (passing the urlopen object) fails as the object does not support .seek.
There is example code on this StackOverflow post for how to wrap the opened object into a file-like object supporting .seek using io.BytesIO (replacement for StringIO).
from PIL import Image
import urllib2 as urllib
import io
fd = urllib.urlopen("http://a/b/c")
image_file = io.BytesIO(fd.read())
im = Image.open(image_file)
The relevant bit of PIL code can be inserted as follows in Image.imread (line 2242) immediately before the prefix load:
The general approach that works is checking for AttributeError during .seek(0) and on failure attempting to wrap the object into a io.Bytes object. This will fail if it is not a buffer and we can exit as normal. Using this we can then request files via urls, using both urllib or requests as follows:
It would be nice if requests.get(url).content worked, but that is a raw bytes object with no .read() so would require an alternative handling.
Is this something that would be considered for inclusion in the PIL library? I am happy to put together a PR if it looks reasonable. This depends on the io module as is which adds a 2.6 requirement, but the earlier StringIO is equivalent (there is a wrapper available in six.BytesIO).
The text was updated successfully, but these errors were encountered:
mfitzp
changed the title
Support for
Support for HTTP request objects as file-like pointers for Image.open()
Mar 26, 2015
Great! I've submitted a PR for consideration. This uses io.BytesIO as that is supported from Python 2.6+ according to the docs.
mfitzp
changed the title
Support for HTTP request objects as file-like pointers for Image.open()
Support for HTTP response objects as file-like pointers for Image.open()
Mar 26, 2015
Opening this in response to this report on matplotlib. Attempting to open remote image files (passing the
urlopen
object) fails as the object does not support.seek
.There is example code on this StackOverflow post for how to wrap the opened object into a file-like object supporting
.seek
usingio.BytesIO
(replacement forStringIO
).The relevant bit of PIL code can be inserted as follows in
Image.imread
(line2242
) immediately before the prefix load:The general approach that works is checking for
AttributeError
during.seek(0)
and on failure attempting to wrap the object into aio.Bytes
object. This will fail if it is not a buffer and we can exit as normal. Using this we can then request files via urls, using bothurllib
orrequests
as follows:It would be nice if
requests.get(url).content
worked, but that is a raw bytes object with no.read()
so would require an alternative handling.Is this something that would be considered for inclusion in the PIL library? I am happy to put together a PR if it looks reasonable. This depends on the
io
module as is which adds a 2.6 requirement, but the earlier StringIO is equivalent (there is a wrapper available insix.BytesIO
).The text was updated successfully, but these errors were encountered: