How to download an image from a URL in Python
Last Updated :
28 Apr, 2025
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.
Similar Reads
How to Download All Images from a Web Page in Python? Prerequisite: Requests BeautifulSouposFile Handling Web scraping is a technique to fetch data from websites. While surfing on the web, many websites donât allow the user to save data for personal use. One way is to manually copy-paste the data, which both tedious and time-consuming. Web Scraping is
3 min read
How to download Google Images using Python Python is a multi-purpose language and widely used for scripting. We can write Python scripts to automate day-to-day things. Letâs say we want to download google images with multiple search queries. Instead of doing it manually we can automate the process. How to install needed Module :Â pip install
2 min read
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 ins
1 min read
How to Download Files from Urls With Python Here, we have a task to download files from URLs with Python. In this article, we will see how to download files from URLs using some generally used methods in Python. Download Files from URLs with PythonBelow are the methods to Download files from URLs with Python: Using 'requests' ModuleUsing 'url
2 min read
How to Download IMAGE From Google Docs (7 Methods) Downloading an image from Google Docs may not seem straight at first, but it can be easily done with a few simple steps. Imagine working on a collaborative project where you need to save images or download a photo from Google Docs for presentations or reports. While Google Docs doesnât provide a dir
10 min read
How to extract images from PDF in Python? The task in this article is to extract images from PDFs and convert them to Image to PDF and PDF to Image in Python.To extract the images from PDF files and save them, we use the PyMuPDF library. First, we would have to install the PyMuPDF library using Pillow.pip install PyMuPDF PillowPyMuPDF is us
3 min read
How To Upload And Download Files From AWS S3 Using Python? Pre-requisite: AWS and S3Amazon Web Services (AWS) offers on-demand cloud services which means it only charges on the services we use (pay-as-you-go pricing). AWS S3 is a cloud storage service from AWS. S3 stands for 'Simple Storage Service. It is scalable, cost-effective, simple, and secure. We gen
3 min read
How to Open URL in Firefox Browser from Python Application? In this article, we'll look at how to use a Python application to access a URL in the Firefox browser. To do so, we'll use the webbrowser Python module. We don't have to install it because it comes pre-installed. There are also a variety of browsers pre-defined in this module, and for this article,
2 min read
Download Google Image Using Python and Selenium In this article, we are going to see how to download google Image using Python and Selenium. Installation On the terminal of your PC, type the following command. If it triggers any error regarding pip then you need to 1st install pip on windows manually by python get-pip.py command then you can run
3 min read