How do I open an image from the internet in PIL?

Viewed 38852

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?

7 Answers

Using requests library and and get output as Bytes

import requests
import io

response = requests.get("https://i.imgur.com/ExdKOOz.png")
image_bytes = io.BytesIO(response.content)
Related