如何在PIL中打开互联网上的图像? [英] How do I open an image from the internet in PIL?

查看:86
本文介绍了如何在PIL中打开互联网上的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在互联网上找到图像的尺寸.我尝试使用

I would like to find the dimensions of an image on the internet. I tried using

from PIL import Image
import urllib2 as urllib
fd = urllib.urlopen("http://a/b/c")
im = Image.open(fd)
im.size

根据此答案中的建议,但我收到错误消息

as suggested in this answer, but I get the error message

addinfourl instance has no attribute 'seek'

我检查了一下,发现urllib2.urlopen(url)返回的对象似乎没有按照dir进行查找的方法.

I checked and objects returned by urllib2.urlopen(url) do not seem to have a seek method according to dir.

那么,我要怎么做才能将来自Internet的图像加载到PIL中?

So, what do I have to do to be able to load an image from the Internet into PIL?

推荐答案

您可以考虑使用 io.BytesIO 以获得前向兼容性.
Python 3中不存在StringIO和cStringIO模块.

You might consider using io.BytesIO for forward compatibility.
The StringIO and cStringIO modules do not exist in Python 3.

from PIL import Image
import urllib2 as urllib
import io

fd = urllib.urlopen("http://a/b/c")
image_file = io.BytesIO(fd.read())
im = Image.open(image_file)

这篇关于如何在PIL中打开互联网上的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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