How to download an image using requests in Python
This tutorial is for How to download an image using requests in Python. Download an image from a URL and saves the image to a local file. we use requests.get() to download an image. simple call requests.get(url) to the address of the file and save the response to a variable.
We use open(filename, mode) (mode as “wb”) to open a stream of the filename in write-and-binary mode. file.write(binary) (binary as “the content of the response”) to write it to a file. And file.close() to close the stream.
requests.get()
import requests
response = requests.get("https://example.com/filename.png")
file = open("devnote.png", "wb")
file.write(response.content)
file.close()
urllib
import urllib.request as req
url ="https://example.com/filename.png"
req.urlretrieve(url, "devnote.png")