Python Download Image from URL: Code Example

You can use the requests library in Python to download images. Here is an example code:

import requests

# 图片的url
url = "https://example.com/image.jpg"

# 发送请求获取图片数据
response = requests.get(url)

# 将图片保存到本地
with open("image.jpg", "wb") as file:
    file.write(response.content)

print("图片下载完成")

In this example, we start by using the requests library to send a GET request to retrieve the image data. Next, we write the retrieved image data to a file called “image.jpg” and finally print “Image download complete.” You can save the above code as a Python script and run it to download the image.

bannerAds