Saturday, October 4, 2025
HomeLanguagesHow to open an image from the URL in PIL?

How to open an image from the URL in PIL?

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 pillow
  • Copy the URL of any image.
  • Write URL with file name in urllib.request.urlretrieve() method.
  • Use Image.open() method to open image.
  • At last show the image using obj.show() method.

Sample Code: 

import urllib.request

from PIL import Image

urllib.request.urlretrieve(‘Image url’, “file_name”)

img = Image.open(“file_name”)

img.show()

Example:

Python3




# importing modules
import urllib.request
from PIL import Image
  
urllib.request.urlretrieve(
   "gfg.png")
  
img = Image.open("gfg.png")
img.show()


Output:

RELATED ARTICLES

Most Popular

Dominic
32337 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6706 POSTS0 COMMENTS
Nicole Veronica
11870 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11934 POSTS0 COMMENTS
Shaida Kate Naidoo
6821 POSTS0 COMMENTS
Ted Musemwa
7086 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6778 POSTS0 COMMENTS