- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to open an image from the URL in PIL?
PIL (Python Imaging Library) is a widely used Python library that enables developers to work with image files. It offers a broad range of functionalities for manipulating and handling image files, such as opening and resizing them, converting between different formats, and more.
One frequently encountered task when working with images involves opening an image file from a URL. This is especially useful when working with images that are stored on a remote server, such as those obtained from an online database or a website.
In this article, we will learn how to open an image from the URL in PIL. We will see the different methods to open an image from a URL in PIL including the following methods −
Using the urllib module
Using the requests module
Using the io module
Using the Pillow module
Different Methods
Below are some of the common methods used in opening an image from the URL in PIL −
Method 1: Using the Urllib Module
The first method used to open an image from the URL in PIL is by using the urlib module to download the image.
Example
Below example demonstrates how to open an image from URL using urllib module in PIL.
In the given example, we first import the urllib.request module and the Image class from PIL. We then specify the URL of the image that we want to open.
We use the urlretrieve() function of the urllib.request module to download the image from the URL and save it to the local filesystem with the filename "image.jpg". This function takes two arguments: the URL of the image and the filename to save it as.
We then use the Image.open() function to open the saved image in PIL, and the show() method to display the image.
#imports import urllib.request from PIL import Image my_url = "https://www.tutorialspoint.com/images/logo.png" # Download the image using urllib urllib.request.urlretrieve(my_url, "logo.png") # Open the downloaded image in PIL my_img = Image.open("logo.png") # Show the image my_img.show()
Output
Method 2: Using Requests Module
The second method used to open an image from the URL in PIL is by using the requests module to download the image.
Example
Below example demonstrates how to open an image from URL using requests module in PIL.
In this example, we first import the requests module and the Image class from PIL. We then specify the URL of the image that we want to open.
We use the requests.get() function to send an HTTP GET request to the URL and retrieve the response. This function returns a Response object that contains the content of the response.
We then use the BytesIO() function from the io module to create a new in-memory binary stream, and pass the content of the response to it. We then use the Image.open() function to open the binary stream as an image in PIL, and the show() method to display the image.
#imports import requests from PIL import Image my_url = "https://www.tutorialspoint.com/images/logo.png" # Download the image using requests my_res = requests.get(my_url) # Open the downloaded image in PIL my_img = Image.open(BytesIO(my_res.content)) # Show the image my_img.show()
Output
Method 3: Using io Module
The third method used to open an image from the URL in PIL is by using the io module to read the image directly from the URL. Below is the syntax for using the io module −
Example
Below example demonstrates how to open an image from URL using io module in PIL.
In this example, to retrieve a response from the URL, we utilize the urllib.request.urlopen() function, which sends an HTTP GET request. After successfully executing this function, we receive a file-like object that holds the content of the response, which we can read at our convenience.
Using a with statement, we take care to close the file-like object after reading its content. Our next step involves using the read() method to extract the content of the response and store it in a variable called img_data.
Using the Image.open() function, we display the image after creating a new in-memory binary stream with the BytesIO() function and passing the img_data variable to it. This process opens the binary stream as an image in PIL.
#imports import urllib.request from PIL import Image from io import BytesIO my_url = "https://www.tutorialspoint.com/images/logo.png" # Read the image from the URL using the io module with urllib.request.urlopen(my_url) as my_url_res: my_img_data = my_url_res.read() # Open the image in PIL my_img = Image.open(BytesIO(my_img_data)) # Show the image my_img.show()
Output
Method 4: Using Pillow Module
The fourth and last method used to open an image from the URL in PIL is by using the Pillow module to open the image directly from the URL.
Example
Below example demonstrates how to open an image from URL using pillow module in PIL.
In this example, to fetch a response, we utilize the requests.get() function, which sends an HTTP GET request to the URL. To prevent immediate storage of the response in memory, we specify the stream parameter as True.
Using the Response object that requests.get() returns, we extract the raw content that we require. Once we have this, we can use Image.open() to access the picture in PIL format. Then, we can employ the show() method to view the image.
#imports from PIL import Image import requests my_url = "https://www.tutorialspoint.com/images/logo.png" # Open the image directly from the URL using Pillow my_img = Image.open(requests.get(my_url, stream=True).raw) # Show the image my_img.show()