2024-10-01 02:18:20 发布
网友
我目前正在使用
import urllib urllib.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")
有没有办法看看链接是否包含图片,如果没有那么就不需要下载,如果有,那么就下载。你知道吗
谢谢!你知道吗
扩展名并不意味着文件是实际图像,如果要检查文件是否确实是图像,可以使用imagemagik identify:
from subprocess import check_output, CalledProcessError from tempfile import NamedTemporaryFile import requests from shutil import move r = requests.get("http://www.digimouth.com/news/media/2011/09/google-logo.jpg").content tmp = NamedTemporaryFile("wb", delete=False, dir=".") tmp.write(r) try: out = check_output(["identify", "-format", "%m", tmp.name]) print(out) move(tmp.name, "whatever.{}".format(out.lower())) except CalledProcessError: tmp.delete = True
要查看所有支持的格式,请运行identify -list format。你知道吗
identify -list format
扩展名并不意味着文件是实际图像,如果要检查文件是否确实是图像,可以使用imagemagik identify:
要查看所有支持的格式,请运行
identify -list format
。你知道吗相关问题 更多 >
编程相关推荐