如何在python中复制远程图像? [英] How do I copy a remote image in python?

查看:28
本文介绍了如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆