To Download Image using requests Library in Python:
- Use the response.raw method
- Use the requests.get() function and Copy image Data
- Use the response.content method to Download Image
- Use the requests and Pil Library
Method No 1: Using the requests library to Download Image
We will apply the first method in this section. We will use the response.raw object to get the image data and then copy it to a file using the Python shutil library.
To download an Image using the requests Library Follow these steps:
- import requests and shutil libraries
- Make a GET request to the URL
- Open the file and Put the Raw content of Image in it
An example that downloads an image from a URL using the requests library is given below:
import requests import shutil img_url = 'https://file-examples.com/storage/fefbfe84f862d721699d168/2017/10/file_example_PNG_500kB.png' path = 'download.png' r = requests.get(img_url, stream=True) if r.status_code == 200: with open(path, 'wb') as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f) print('Image Downloaded Successfully')
Method No 2: Image Download using requests library in Python
To download an image using the requests library we need to put it in some file so we will use the shutil module along with the requests library. We will make an HTTP request using the requests library and then download it to a file:
import requests import shutil img_url = 'https://file-examples.com/storage/fefbfe84f862d721699d168/2017/10/file_example_PNG_500kB.png' path = 'download1.png' response = requests.get(img_url, stream=True) with open('img.png', 'wb') as out_file: shutil.copyfileobj(response.raw, out_file) del response
Method No 3: Download Image using Python requests library
In this method, we will use the response.content and put it in a file using the built-in write() function. The write() function is used to write data to a file. See the following example to download images in Python.
import requests import shutil img_url = 'https://file-examples.com/storage/fefbfe84f862d721699d168/2017/10/file_example_PNG_500kB.png' path = 'download2.png' response = requests.get(img_url) if response.status_code == 200: with open(path, 'wb') as f: f.write(response.content)
Method No 4: Use the requests and PIL Library in Python to Download a file
The most recommended way to download an image from an online source is to use the requests library along with the PIL library. PIL library is used for Image manipulation. PIL library is one of the best python image Manipulation Libraries.
import requests from PIL import Image r = requests.get('https://file-examples.com/storage/fefbfe84f862d721699d168/2017/10/file_example_PNG_500kB.png', stream=True) r.raise_for_status() r.raw.decode_content = True with Image.open(r.raw) as img: img.show() r.close()
A quick Guide to Python requests library
Here are a few more use cases of using python requests module. I hope you will like them.
Example No 1 : Using requests library to upload file to server
import requests files=[('file', open('client.py'))] URL = 'http://0.0.0.0:8080/test' r = requests.post(URL, files=files) print(r.text)