Menu Close

How to download image using requests with Python?

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.

Posted in Python, Python Answers