What I'm trying to do is fairly simple when we're dealing with a local file, but the problem comes when I try to do this with a remote URL.
Basically, I'm trying to create a PIL image object from a file pulled from a URL. Sure, I could always just fetch the URL and store it in a temp file, then open it into an image object, but that feels very inefficient.
Here's what I have:
Image.open(urlopen(url))
It flakes out complaining that seek()
isn't available, so then I tried this:
Image.open(urlopen(url).read())
But that didn't work either. Is there a Better Way to do this, or is writing to a temporary file the accepted way of doing this sort of thing?
For those doing some sklearn/numpy post processing (i.e. Deep learning) you can wrap the PIL object with np.array(). This might save you from having to Google it like I did:
This works on Python 3.6 as well...
select the image in chrome, right click on it, click on
Copy image address
, paste it into astr
variable (my_url
) to read the image:open it;
In Python3 the StringIO and cStringIO modules are gone.
In Python3 you should use:
you could try using a StringIO
I use the requests library. It seems to be more robust.