如何在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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
下载:
要验证可以使用 PIL
如果您只是想验证这是一个即使图像数据无效:您可以使用imghdr
方法检查标题并确定图像类型。如果图像无法识别,它将返回 None。
To download:
To verify can use PIL
If you just want to verify this is an image even if the image data is not valid: You can use imghdr
The method checks the headers and determines the image type. It will return None if the image was not identifiable.
下载东西
可以通过多种方式验证它是否是图像。最难的检查是使用 Python 图像库打开文件并查看它是否抛出错误。
如果您想在下载前检查文件类型,请查看远程服务器提供的 mime 类型。
Downloading stuff
Verifying that it is a image can be done in many ways. The hardest check is opening the file with the Python Image Library and see if it throws an error.
If you want to check the file type before downloading, look at the mime-type the remote server gives.
使用 httplib2 同样的事情...
Same thing using httplib2...
对于问题中有关复制远程图像的部分,这是受此答案:
请注意,此方法适用于复制任何二进制媒体类型的远程文件。
For the portion of the question with respect to copying a remote image, here's an answer inspired by this answer:
Note that this approach will work for copying a remote file of any binary media type.