wx.Image from image URL

时间:2013-12-23 02:23:07

标签: python wxpython urllib2

好吧,我一直在做大量的搜索,似乎没有太多人在谈论这个问题。借助wxPython的文档,我已经做到了这一点。我想从图像URL创建一个wx.Image小部件。 http://wxpython.org/docs/api/wx-module.html#ImageFromStream

imgStream = urllib2.urlopen(captchaURL).read()
captchaImg = wx.Image(wx.ImageFromStream(wx.InputStream(imgStream)), wx.BITMAP_TYPE_ANY)

有人对我有一些建议吗?非常感谢。

哦,我得到的错误是TypeError:不是类似文件的对象,上面的代码片段。

2 个答案:

答案 0 :(得分:0)

buf = urllib2.urlopen(URL).read()
sbuf = StringIO.StringIO(buf)
Image = wx.ImageFromStream(sbuf).ConvertToBitmap()
wx.StaticBitmap(Panel, -1, Image, (10,10))

答案 1 :(得分:0)

import requests
import io

url = ""
content = requests.get(url).content
io_bytes = io.BytesIO(content)
image = wx.Image(io_bytes).ConvertToBitmap()
staticImage = wx.StaticBitmap(Panel, wx.ID_ANY, image, wx.DefaultPosition, wx.DefaultSize, 0)

用您的网址和面板对象替换urlPanel