在Python中从NSImage中提取位图数据
我正在使用 NSImage 中的数据构建 wx.MemoryDC,但生成的代码非常缓慢。在我看来,TIFFRepresentation -> ImageFromStream 步骤是减慢速度的一步。有没有办法避免这一步(所有这些流),并直接从 NSImage 数据初始化 MemoryDC ?这是示例代码:
import wx
import cStringIO
from AppKit import NSImage
app = wx.PySimpleApp()
frame = wx.Frame(None, wx.ID_ANY, "Python")
static_bitmap = wx.StaticBitmap(frame,wx.NewId(), bitmap=wx.EmptyBitmap(640, 480))
frame.Show(True)
# wget http://upload.wikimedia.org/wikipedia/commons/d/d9/Test.png
ns_image = NSImage.alloc().initWithContentsOfFile_("Test.png")
for i in range(100):
tiffdata = ns_image.TIFFRepresentation()
image = wx.ImageFromStream(cStringIO.StringIO(tiffdata), wx.BITMAP_TYPE_TIF)
bitmap = image.ConvertToBitmap()
bmdc = wx.MemoryDC(bitmap)
# bmdc.DrawCircle(10,10, 5)
del bmdc
static_bitmap.SetBitmap(bitmap)
app.MainLoop()
I'm constructing wx.MemoryDC using the data from the NSImage, but the resulting code is very sluggish. It seems to me that TIFFRepresentation -> ImageFromStream step is the one that slows things down. Is there any way to avoid this step (all this streaming), and initialize MemoryDC directly from the NSImage data? Here is the sample code:
import wx
import cStringIO
from AppKit import NSImage
app = wx.PySimpleApp()
frame = wx.Frame(None, wx.ID_ANY, "Python")
static_bitmap = wx.StaticBitmap(frame,wx.NewId(), bitmap=wx.EmptyBitmap(640, 480))
frame.Show(True)
# wget http://upload.wikimedia.org/wikipedia/commons/d/d9/Test.png
ns_image = NSImage.alloc().initWithContentsOfFile_("Test.png")
for i in range(100):
tiffdata = ns_image.TIFFRepresentation()
image = wx.ImageFromStream(cStringIO.StringIO(tiffdata), wx.BITMAP_TYPE_TIF)
bitmap = image.ConvertToBitmap()
bmdc = wx.MemoryDC(bitmap)
# bmdc.DrawCircle(10,10, 5)
del bmdc
static_bitmap.SetBitmap(bitmap)
app.MainLoop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用:
而不是
ImageFromStream
。Try using:
instead of
ImageFromStream
.回答我自己的问题:NSIimage 的接口本质上很慢,唯一可行的解决方案是完全避免它。
Answering my own question: interface to NSIimage is inherently slow, the only workable solution is to avoid it altogether.