How to download image using requests Library in Python

0

To Download Image using requests Library in Python:

  1. Use the response.raw method
  2. Use the requests.get() function and Copy image Data
  3. Use the response.content method to Download Image
  4. Use the requests and Pil Library

Download Images in Python

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:

  1. import requests and shutil libraries
  2. Make a GET request to the URL
  3. 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)


Have a Chat With Me!

I hope you liked this Python Tutorialđź’—. Now, Don't hesitate to leave a message at haxratali0@gamil.com. I respond to every mail I receive. I will be more than happy to chat with you.

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !