Monday, November 18, 2024
Google search engine
HomeLanguagesHow to download an image from a URL in Python

How to download an image from a URL in Python

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 neveropen.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 neveropen.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, "neveropen.png")
  
# Opening the image and displaying it (to confirm its presence)
img = Image.open(r"neveropen.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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments