如何在python中复制远程映像? [英] How do I copy a remote image in python?
本文介绍了如何在python中复制远程映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将远程图像(例如 http://example.com/image.jpg
)复制到我的服务器。这是可能的吗?
I need to copy a remote image (for example http://example.com/image.jpg
) to my server. Is this possible?
如何验证这确实是一个图像?
How do you verify that this is indeed an image?
推荐答案
要下载:
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
如果您只想验证即使图像数据无效,也是图像:您可以使用 imghdr
import imghdr
imghdr.what('ignore', img)
该方法检查标题并确定图像类型。如果图像不可识别,它将返回None。
The method checks the headers and determines the image type. It will return None if the image was not identifiable.
这篇关于如何在python中复制远程映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文