Open In App

How to download an image from a URL in Python

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Downloading content from its URL is a common task that Web Scrapers or online trackers perform. These URLs or Uniform Resource Locators can contain the web address (or local address) of a webpage, website, image, text document, container files, and many other online resources. It is quite easy to download and store content from files on the internet. This article will teach you how to download an image from a URL in Python.

Later, we would be using the pillow library to display the downloaded image (to confirm its presence). Which could be installed into the Python distribution using:

!pip install requests
!pip install pillow

* The process of displaying the image is optional and does not add anything to the problem at hand other than the clarity of results it produces

Downloading an image using urllib.request library

Firstly the relevant libraries are imported. Then a call to the web address is made, and the resource returned in the response is stored in the file named geeksforgeeks.png. It should be noted that the extension of the image file has to be known here, i.e., the container for the image file format, such as png, jpg, SVG, BMP, etc., needs to be known beforehand. In this case, the image was in PNG format, which was mentioned in the output file name. After the aforementioned function has been executed successfully, a file named geeksforgeeks.png will be produced in the current working directory of the program. This file is later opened using the Image.open function and, in the end, displayed using the Image.show function.

Python3




import urllib.request
from PIL import Image
  
# Retrieving the resource located at the URL
# and storing it in the file name a.png
url = "https://media.geeksforgeeks.org/\
wp-content/uploads/20210224040124/\
JSBinCollaborativeJavaScriptDebugging6-300x160.png"
urllib.request.urlretrieve(url, "geeksforgeeks.png")
  
# Opening the image and displaying it (to confirm its presence)
img = Image.open(r"geeksforgeeks.png")
img.show()


Output:

 

Downloading an image using the requests library

Downloading an image into python could also be performed by using the request library. The library is a part of the default python distribution. 

Python3




import requests
from PIL import Image
  
url = 'https://media.geeksforgeeks.org/\
wp-content/uploads/20210224040124/JSBinColla\
borativeJavaScriptDebugging6-300x160.png'
  
# This statement requests the resource at
# the given link, extracts its contents
# and saves it in a variable
data = requests.get(url).content
  
# Opening a new file named img with extension .jpg
# This file would store the data of the image file
f = open('img.jpg','wb')
  
# Storing the image data inside the data variable to the file
f.write(data)
f.close()
  
# Opening the saved image and displaying it
img = Image.open('img.jpg')
img.show()


Output:

 

Firstly, the relevant libraries were imported. Then a variable named URL is assigned the address from where the image file is to be downloaded. Then a request to the URL using the function requests.get is made. Then the contents from the response are extracted. A new file is opened named img.jpg (make sure the file extension is the same as the downloaded file). The data that was obtained by the get function is stored in this file. Later this file is opened via PIL and displayed.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads