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

15

316

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?

Villagomez answered 12/9, 2011 at 18:1 Comment(2)
See also: How to save an image locally using Python whose URL address I already know?Wicker
There must be an issue where the requests is not able to fetch the image from the url. Try the same ( just for testing purpose) from another url.Comte
C
196

The following works for Python 3:

from PIL import Image
import requests

im = Image.open(requests.get(url, stream=True).raw)

References:

Counseloratlaw answered 3/12, 2016 at 4:14 Comment(3)
urllib2 was for Python2 I think, which is outdated. For Python 3 it's urllib.requests: urllib.request.urlopen(url).read()Groundhog
As mentioned by @Groundhog urllib is outdated. I used the second option as I was using 'requests' anyway in my code and it worked, so upvoting. Should the urllib part of the solution be removed so that readers don't spend time on trying the first approach just to realize that it doesn't work and then move to the next one?Epeirogeny
Hi this worked great for my project! Just wondering, does this build up any buffer or cache? Do I need to close these images / clear anything?Mandolin
A
452

In Python3 the StringIO and cStringIO modules are gone.

In Python3 you should use:

from PIL import Image
import requests
from io import BytesIO

response = requests.get(url)
img = Image.open(BytesIO(response.content))
Atc answered 6/5, 2014 at 8:21 Comment(6)
How to get back the image from response.content ?Inlet
requests package throws 503 status code while fetching an image from a URL. Instead, I had to resort to http.client to get the image.Sidman
When I try this I get: AttributeError: module 'requests' has no attribute 'get'.Oophorectomy
Manually wrapping in BytesIO is no longer needed since PIL >= 2.8.0. Just use Image.open(response.raw). PIL automatically checks for that now and does the BytesIO wrapping under the hood. From: pillow.readthedocs.io/en/3.0.x/releasenotes/2.8.0.htmlGainor
@ViníciusM your answer should be at the top! thank youSurfacetosurface
Kinda annoying that 3 libraries are necessary... Pillow should just add this functionality!Tecla
C
196

The following works for Python 3:

from PIL import Image
import requests

im = Image.open(requests.get(url, stream=True).raw)

References:

Counseloratlaw answered 3/12, 2016 at 4:14 Comment(3)
urllib2 was for Python2 I think, which is outdated. For Python 3 it's urllib.requests: urllib.request.urlopen(url).read()Groundhog
As mentioned by @Groundhog urllib is outdated. I used the second option as I was using 'requests' anyway in my code and it worked, so upvoting. Should the urllib part of the solution be removed so that readers don't spend time on trying the first approach just to realize that it doesn't work and then move to the next one?Epeirogeny
Hi this worked great for my project! Just wondering, does this build up any buffer or cache? Do I need to close these images / clear anything?Mandolin
C
173

Using a StringIO

import urllib, cStringIO

file = cStringIO.StringIO(urllib.urlopen(URL).read())
img = Image.open(file)
Certainly answered 12/9, 2011 at 18:7 Comment(3)
Thanks, would just like to add that the same exact code will work with urllib2 (with Python2)Monegasque
in python 3 it would be from urllib.request import urlopen and io.io.BytesIO instead of StringIOMyrtia
HELP, IOError: cannot identify image file <_io.BytesIO object at 0x7fb91b6a29b0> my url is: ...model=product.template&id=16&field=image_mediumFecundity
R
63

Using requests:

from PIL import Image
import requests
from StringIO import StringIO

response = requests.get(url)
img = Image.open(StringIO(response.content))
Regal answered 23/10, 2012 at 6:19 Comment(6)
For some reason urllib didn't work for some URLs, but requests worked where that failedSixteenmo
I couldn't find the PIL package, but it looks like pillow have taken over the PIL effort and you can install for python3 with pip3.4 install pillow.Kosse
Note that requests will load the entire response into memory, and then PIL will load the entire thing again as an image, so you have two full copies resident in memory. The previous answer using urllib method streams the data, so you only end up with one copy plus the streaming buffer size. You can stream the data with requests too, but because the response does not support read() semantics, you would have to build an adapter.Thibodeau
@Thibodeau Are you referring to urllib2 or urllib?Disintegrate
@Disintegrate I was referring to the accepted urllib answer. If memory overhead is a concern, it is better than using this requests answer. (However, like I mentioned, a different solution using requests could achieve the same effect.)Thibodeau
@Thibodeau PIL Apparently now supports the streaming too. =) pillow.readthedocs.io/en/3.0.x/releasenotes/2.8.0.htmlGainor
H
52

Python 3

from urllib.request import urlopen
from PIL import Image

img = Image.open(urlopen(url))
img

Jupyter Notebook and IPython

import IPython
url = 'https://newevolutiondesigns.com/images/freebies/colorful-background-14.jpg'
IPython.display.Image(url, width = 250)

Unlike other methods, this method also works in a for loop!

Hahnemann answered 5/9, 2018 at 5:18 Comment(0)
M
31

This answer was written for Python 2.7.

For Python 3, urlopen was moved from urllib to urllib.requests. And StringIO.StringIO was replaced by io.BytesIO.

Use StringIO to turn the read string into a file-like object:

from StringIO import StringIO
from PIL import Image
import urllib

Image.open(StringIO(urllib.urlopen(url).read()))
Mountfort answered 12/9, 2011 at 18:6 Comment(2)
This response is clean and helpful, but the import statement should read from io import StringIOAbridge
for Python 3, one would also need BytesIO not StringIO here: from io import BytesIOStaats
L
28

The arguably recommended way to do image input/output these days is to use the dedicated package ImageIO. Image data can be read directly from a URL with one simple line of code:

from imageio import imread
image = imread('https://cdn.sstatic.net/Sites/stackoverflow/img/logo.png')

Many answers on this page predate the release of that package and therefore do not mention it. ImageIO started out as component of the Scikit-Image toolkit. It supports a number of scientific formats on top of the ones provided by the popular image-processing library PILlow. It wraps it all in a clean API solely focused on image input/output. In fact, SciPy removed its own image reader/writer in favor of ImageIO.

Ladder answered 18/6, 2019 at 13:21 Comment(2)
Very slow. skimage methods would be better option if you want to do in one lineWedding
This is the skimage (Scikit-Image) method, as the answer explains. And it's as slow as your internet connection.Ladder
B
24

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:

from PIL import Image
import requests
import numpy as np
from StringIO import StringIO

response = requests.get(url)
img = np.array(Image.open(StringIO(response.content)))
Bumkin answered 17/10, 2015 at 15:59 Comment(0)
D
5

select the image in chrome, right click on it, click on Copy image address, paste it into a str variable (my_url) to read the image:

import shutil
import requests

my_url = 'https://www.washingtonian.com/wp-content/uploads/2017/06/6-30-17-goat-yoga-congressional-cemetery-1-994x559.jpg'
response = requests.get(my_url, stream=True)
with open('my_image.png', 'wb') as file:
    shutil.copyfileobj(response.raw, file)
del response

open it;

from PIL import Image

img = Image.open('my_image.png')
img.show()
Davy answered 30/5, 2018 at 20:46 Comment(0)
L
4

Manually wrapping in BytesIO is no longer needed since PIL >= 2.8.0. Just use Image.open(response.raw)

Adding on top of Vinícius's comment:

You should pass stream=True as noted https://requests.readthedocs.io/en/master/user/quickstart/#raw-response-content

So

img = Image.open(requests.get(url, stream=True).raw)
Lowlife answered 24/8, 2020 at 18:39 Comment(0)
B
3

USE urllib.request.urlretrieve() AND PIL.Image.open() TO DOWNLOAD AND READ IMAGE DATA :

import requests
import urllib.request
import PIL

urllib.request.urlretrieve("https://i.imgur.com/ExdKOOz.png", "sample.png")
img = PIL.Image.open("sample.png")
img.show()

or Call requests.get(url) with url as the address of the object file to download via a GET request. Call io.BytesIO(obj) with obj as the content of the response to load the raw data as a bytes object. To load the image data, call PIL.Image.open(bytes_obj) with bytes_obj as the bytes object:

import io

response = requests.get("https://i.imgur.com/ExdKOOz.png")
image_bytes = io.BytesIO(response.content)
img = PIL.Image.open(image_bytes)
img.show()
Bathilda answered 10/12, 2020 at 14:40 Comment(0)
B
3
from PIL import Image
import cv2
import numpy as np
import requests
image=Image.open(requests.get("https://previews.123rf.com/images/darrenwhi/darrenwhi1310/darrenwhi131000024/24022179-photo-of-many-cars-with-one-a-different-color.jpg", stream=True).raw)
#image =resize((420,250))

image_array=np.array(image)
image 
Bathilda answered 16/11, 2021 at 13:9 Comment(0)
C
2

For Python 3 using OpenCV:

import cv2
from urllib.request import urlopen

image_url = "IMAGE-URL-GOES-HERE"
resp = urlopen(image_url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR) # The image object

# Optional: For testing & viewing the image
cv2.imshow('image',image)

For Python 3 using OpenCV and Google Colab/Jupyter Notebook:

import cv2
from google.colab.patches import cv2_imshow
from urllib.request import urlopen

image_url = "IMAGE-URL-GOES-HERE"
resp = urlopen(image_url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR) # The image object

# Optional: For testing & viewing the image
cv2_imshow(image)
Centurion answered 27/4, 2023 at 9:53 Comment(0)
M
1

To directly get image as numpy array without using PIL

import requests, io
import matplotlib.pyplot as plt 

response = requests.get(url).content
img = plt.imread(io.BytesIO(response), format='JPG')
plt.imshow(img)
Mcghee answered 24/10, 2020 at 3:10 Comment(0)
R
0

The solutions mentioned above might work, but it misses one point that I would like to highlight i.e. when we fetch or retrieve the image url to read, we might not always get the actual image content if we don't pass the headers while making the get request.

for example:

request without Headers

import requests
url = "https://www.roaringcreationsfilms.com/rcsfilms-media/chankya-quotes-in-hindi-32.jpg"
data = requests.get(url).content

if we check the data:

print(data)
b'<head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An 
appropriate representation of the requested resource could not be found on this server.
This error was generated by Mod_Security.</p></body></html>'

you see, we don't actually get the content of the image.

request with Headers

import requests
url = "https://www.roaringcreationsfilms.com/rcsfilms-media/chankya-quotes-in-hindi-32.jpg"
headers = {"User-Agent": "PostmanRuntime/7.31.1"}
data = requests.get(url, headers=headers).content

and, if we now check the data:

print(data)
b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\t\x06\x06\........\xfb\x04El\xb3\xa8L\xbc\xa12\xc6<\xc4\x891\xf2L|\xf7\x9eV\x18\xc5\xd8\x8f\x02\xca\xdc\xb1c+-\x96\'\x86\xcb,l\xb12;\x16\xd4j\xfd/\xde\xbf\xff\xd9'

Now, we get the actual content of the image.

Things to note are that different urls might require different combinations of the headers (such as "User-Agent", "Accept", "Accept-Encoding", etc.) to successfully get the data and some even might not require any headers. But it's always a good practice to pass "User-Agent" as a minimum required header while making the request.

Rudolf answered 3/3, 2023 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.