I would like to find the dimensions of an image on the internet. I tried using
from PIL import Image import urllib2 as urllib fd = urllib.urlopen("http://a/b/c") im = Image.open(fd) im.size
as suggested in this answer, but I get the error message
addinfourl instance has no attribute 'seek'
I checked and objects returned by urllib2.urlopen(url)
do not seem to have a seek method according to dir
.
So, what do I have to do to be able to load an image from the Internet into PIL?
Image. open() Opens and identifies the given image file. This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load() method).
On your computer, go to images.google.com. Search for the image. In Images results, click the image. At the top of your browser, click the address bar to select the entire URL.
Python Imaging Library is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats. It is available for Windows, Mac OS X and Linux. The latest version of PIL is 1.1.
just open the python interpreter and type webbrowser. open('http://www.google.com') and see if it does what you want.
You might consider using io.BytesIO
for forward compatibility.
The StringIO and cStringIO modules do not exist in Python 3.
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)
Using Python requests
:
from PIL import Image from StringIO import StringIO import requests r = requests.get("http://a/b/c") im = Image.open(StringIO(r.content)) im.size
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With