Python allows you to easily download files using URLs and file locations. Sometimes you may need to download images in Python. There are several Python modules to help you with this. In this article, we will learn how to download images in Python using Requests module.
How to Download Images in Python
Here is a simple code snippet to help you download an image from URL.
import requests url = "<image_url>" response = requests.get(url) if response.status_code == 200: with open("/path/to/file", 'wb') as f: f.write(response.content)
In the above code, we first import requests library that makes it easy to work with requests. We use get() function to download the image, that is, get a response. We check the response code, if it is 200 then we write the file to our desired location. Replace <image_url> and /path/to/file with URL of image and local path to write your file respectively.
Here is an example for the same.
import requests url = "http://example.com/images/test.jpg" response = requests.get(url) if response.status_code == 200: with open("/Users/Desktop/sample.jpg", 'wb') as f: f.write(response.content)
Although requests is more feature-rich than urllib module, in this case it provides a very concise way to download images and even other files.
import urllib urllib.request.urlretrieve("<image_url>", "<local_file_path>")
For example, if you want to download image at http://example.com/image.jpg and write it to /home/ubuntu/image.jpg you can easily do this in 2 lines as shown below.
import urllib urllib.request.urlretrieve("http://www.example.com/image.jpg", "/home/ubuntu/image.jpg")
urlretrieve() function conveniently downloads a file using URL, and writes it to a local path provided as 2nd argument.
If you are comfortable using the good old wget tool from command line, you can also use it from within python environment using wget module, as shown below. You just need to provide URL to image in wget.download() function, and local path to write file, as second argument.
import wget >>> url = 'http://example.com/image.jpg' >>> filename = wget.download(url, out='/home/ubuntu/') 100% [................................................] 3841 / 3841> >> filename 'image.jpg'
In this article, we have learnt three simple ways to download image in Python. You can use any of them as per your requirement.
Also read:
How to Remove Trailing Newline in Python
How to Pad String in Python
How to Get Size of Object in Python
How to Import from Parent Folder in Python
How to Attach Event to Dynamic Elements in JavaScript
Related posts:
How to Remove Trailing Newline in Python
How to Remove Line from File Using Python
How to Shuffle List of Objects in Python
How to Convert CSV to Tab Delimited File in Python
How to Count Occurrence of List Item in Python
How to Drop One or More Columns in Python Pandas
How to Find Local IP Address in Python
How to Get Difference of Two Dictionaries in Python

Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.