Python Read Image From Url

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

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:
It flakes out complaining that seek() isn’t available, so then I tried this:
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?
asked Sep 12 ’11 at 18:01
Daniel QuinnDaniel Quinn5, 0163 gold badges31 silver badges51 bronze badges
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 = (url)
img = (BytesIO(ntent))
answered May 6 ’14 at 8:21
Using a StringIO
import urllib, cStringIO
file = ringIO(urllib. urlopen(URL)())
img = (file)
iacob10. 4k4 gold badges36 silver badges69 bronze badges
answered Sep 12 ’11 at 18:07
Fábio DinizFábio Diniz9, 4553 gold badges33 silver badges45 bronze badges
Using requests:
from StringIO import StringIO
img = (StringIO(ntent))
answered Oct 23 ’12 at 6:19
SauravSaurav2, 9663 gold badges17 silver badges12 bronze badges
from quest import urlopen
img = (urlopen(url))
import IPython
url = ”
(url, width = 250)
Unlike other methods, this method also works in a for loop!
answered Sep 5 ’18 at 5:18
MiladioussMiladiouss2, 90120 silver badges27 bronze badges
Use StringIO to turn the read string into a file-like object:
import urllib
(StringIO(quests. urlopen(url)()))
Asclepius45k14 gold badges130 silver badges113 bronze badges
answered Sep 12 ’11 at 18:06
Dan D. 68. 8k13 gold badges96 silver badges113 bronze badges
For those doing some sklearn/numpy post processing (i. e. Deep learning) you can wrap the PIL object with (). This might save you from having to Google it like I did:
import numpy as np
img = ((StringIO(ntent)))
answered Oct 17 ’15 at 15:59
BenBen4714 silver badges6 bronze badges
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(”)
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.
answered Jun 18 ’19 at 13:21
John HennigJohn Hennig2, 7101 gold badge16 silver badges32 bronze badges
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
my_url = ”
response = (my_url, stream=True)
with open(”, ‘wb’) as file:
pyfileobj(, file)
del response
open it;
img = (”)
answered May 30 ’18 at 20:46
ShividShivid1, 1331 gold badge18 silver badges31 bronze badges
To directly get image as numpy array without using PIL
import requests, io
import as plt
response = (url). content
img = (tesIO(response), format=’JPG’)
answered Oct 24 ’20 at 3:10
USE quest. urlretrieve() AND () TO DOWNLOAD AND READ IMAGE DATA:
import quest
import PIL
quest. urlretrieve(“, “”)
img = (“”)
or Call (url) with url as the address of the object file to download via a GET request. Call tesIO(obj) with obj as the content of the response to load the raw data as a bytes object. To load the image data, call (bytes_obj) with bytes_obj as the bytes object:
import io
response = (“)
image_bytes = tesIO(ntent)
img = (image_bytes)
answered Dec 10 ’20 at 14:40
Not the answer you’re looking for? Browse other questions tagged python python-imaging-library or ask your own question.
How to open an image from the URL in PIL? - GeeksforGeeks

How to open an image from the URL in PIL? – GeeksforGeeks

In this article, we will learn How to open an image from the URL using the PIL module in python. For the opening of the image from a URL in Python, we need two Packages urllib and Pillow(PIL). Approach:Install the required libraries and then import them. To install use the following commands:pip install pillowCopy the URL of any URL with file name in quest. urlretrieve() () method to open last show the image using () Code: Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Courseimport questfrom PIL import quest. urlretrieve(‘Image url’, “file_name”)img = (“file_name”)()Example:Output:
How to read an image from URL in Python - CodeSpeedy

How to read an image from URL in Python – CodeSpeedy

Learn how to read an image from URL in Python with this times a situation may arrive where you need to download images instantly from the internet, and not only one image there are a bunch of images, In this doing manual copy and pasting can be Boring and time-consuming task to do, You need a reliable and faster solution to this, In this tutorial, we will be learning how to read and download images using ‘URL’ in Python. Here we will be using Firstly, the ‘sys’ module so that we can give input URL directly on the command line while running our program. Second, we will be using the ‘Pillow’ library for opening an image as an object and lastly, the most important one is the ‘Requests’ Library of Python for opening and downloading the image from a specified URL. About Requests LibraryRequests library is used for processing HTTP requests in python. This library allows request methods like get, put, post, delete, etc. Some of the features that are supported by the library are: Automatic URL formation consisting of post data using ‘urllib3’. International Domains and URLs can be accessed. SSL Certificate verification. Both Basic and Digest Authentication. Unicode Response bodies. Returns cookies in as learn More about Requests Library you read its detailed to read an image from URL in PythonBefore we start with the actual code first we need to install required libraries or stallation:$ pip3 install pillow
$ pip3 install requestsAfter installation, we can start with the code. I would suggest you first get through the code then I’ll be explaining to you the important Code:# Importing Required Modules
import sys
import requests
from PIL import Image
# Exception Handling for invalid requests
# Creating an request object to store the response
# The URL is refrenced [1]
ImgRequest = ([1])
# Verifying whether the specified URL exist or not
if atus_code ==
# Opening a file to write bytes from response content
# Storing this onject as an image file on the hard drive
img = open(“”, “wb”)
# Opening Inage file using PIL Image
img = (“”)
except Exception as e:
print(str(e))Now here we will be supplying the image URL as command line argument which would we referenced later by the object. After the request has been made the response status code is verified whether it is in the codes range (>200 & <=400). Once the status code is verified then the response content would be written into a binary file and saved as an image file. Later we open it using Pil Module for taking a $ python3 ‘’ is the python source In this way, you can read an image from URL using Python. I hope this tutorial was fruitful to you, Thank you ‘Keep Learning Keep Coding’.

Frequently Asked Questions about python read image from url

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

Use requests. get() and PIL. Image. open() to download and read image dataresponse = requests. get(“”)image_bytes = io. BytesIO(response. content)img = PIL. Image. open(image_bytes)img. show()

How do I read an image URL?

Copy.On your computer, go to for the image.In Images results, click the image.At the top of your browser, click the address bar to select the entire URL.Right-click the selected URL. Copy.

How do I open an image in Python?

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).Jul 17, 2019

Leave a Reply

Your email address will not be published. Required fields are marked *