如何从内存数据创建 wx.Image 对象? [英] How do I create a wx.Image object from in-memory data?

查看:23
本文介绍了如何从内存数据创建 wx.Image 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wxPython 在 Python 中编写 GUI 应用程序,我想在静态控件 (wx.StaticBitmap) 中显示图像.

I'm writing a GUI application in Python using wxPython and I want to display an image in a static control (wx.StaticBitmap).

我可以使用 wx.ImageFromStream 从文件加载图像,这可以正常工作:

I can use wx.ImageFromStream to load an image from a file, and this works OK:

static_bitmap = wx.StaticBitmap(parent, wx.ID_ANY)
f = open("test.jpg", "rb")
image = wx.ImageFromStream(f)
bitmap = wx.BitmapFromImage(image)
static_bitmap.SetBitmap(bitmap)

但是,我真正想做的是从内存中的数据创建图像.所以,如果我写

But, what I really want to be able to do is create the image from data in memory. So, if I write

f = open("test.jpg", "rb")
data = f.read()

如何从 data 创建一个 wx.Image 对象?

how can I create a wx.Image object from data?

感谢您的帮助!

推荐答案

您应该能够使用 StringIO 将缓冲区包装在内存文件对象中.

You should be able to use StringIO to wrap the buffer in a memory file object.

...
import StringIO

buf = open("test.jpg", "rb").read()
# buf = get_image_data()
sbuf = StringIO.StringIO(buf)

image = wx.ImageFromStream(sbuf)
...

buf 可以替换为任何数据字符串.

buf can be replaced with any data string.

这篇关于如何从内存数据创建 wx.Image 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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