如何在python中复制远程映像?
发布于 2021-01-29 17:48:44
我需要将一个远程映像(例如http://example.com/image.jpg
)复制到我的服务器上。这可能吗?
您如何验证这确实是图像?
关注者
0
被浏览
35
1 个回答
-
去下载:
import urllib2 img = urllib2.urlopen("http://example.com/image.jpg").read()
验证可以使用PIL
import StringIO from PIL import Image try: im = Image.open(StringIO.StringIO(img)) im.verify() except Exception, e: # The image is not valid
即使图像数据无效,如果您只想验证这是图像,也
可以使用imghdrimport imghdr imghdr.what('ignore', img)
该方法检查标题并确定图像类型。如果无法识别图像,它将返回无。