How do I read image data from a URL in Python? How do I read image data from a URL in Python? python python

How do I read image data from a URL in Python?


In Python3 the StringIO and cStringIO modules are gone.

In Python3 you should use:

from PIL import Imageimport requestsfrom io import BytesIOresponse = requests.get(url)img = Image.open(BytesIO(response.content))


Using a StringIO

import urllib, cStringIOfile = cStringIO.StringIO(urllib.urlopen(URL).read())img = Image.open(file)


The following works for Python 3:

from PIL import Imageimport requestsim = Image.open(requests.get(url, stream=True).raw)

References: