Categories
Python Answers

How to download image using requests with Python?

Spread the love

Sometimes, we want to download image using requests with Python.

In this article, we’ll look at how to download image using requests with Python.

How to download image using requests with Python?

To download image using requests with Python, we can use the requests.get method.

For instance, we write

import shutil

import requests

url = 'http://example.com/img.png'
response = requests.get(url, stream=True)
with open('img.png', 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)

to call requests.get to make a GET request to the image url.

And then we write the file into the out_file with

shutil.copyfileobj(response.raw, out_file)

after opening img.ong with open.

Conclusion

To download image using requests with Python, we can use the requests.get method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *