Python 批量下载图片链接并保存为 JPG 格式
您可以使用 Python 编程语言来实现将多个图片链接批量导出为 jpg 格式并保存在文件夹中的功能。以下是一个示例代码:
import requests
import os
def save_images(image_urls, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for url in image_urls:
try:
response = requests.get(url, stream=True)
if response.status_code == 200:
image_name = url.split('/')[-1]
image_path = os.path.join(output_folder, image_name)
with open(image_path, 'wb') as f:
for chunk in response.iter_content(1024):
f.write(chunk)
print(f'成功保存图片:{image_path}')
else:
print(f'无法下载图片:{url}')
except Exception as e:
print(f'保存图片时发生错误:{e}')
# 要保存的图片链接列表
image_urls = [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
'https://example.com/image3.jpg',
# 添加更多的图片链接...
]
# 保存图片的文件夹路径
output_folder = 'output'
# 调用函数进行保存
save_images(image_urls, output_folder)
您需要将image_urls列表替换为您要保存的图片链接列表,并将output_folder变量替换为您要保存图片的文件夹路径。运行代码后,程序会自动下载并保存所有图片链接中的图片到指定的文件夹中。请确保您的计算机上已安装 Python 和 requests 库。
原文地址: https://www.cveoy.top/t/topic/ph3Z 著作权归作者所有。请勿转载和采集!