开发者

Extracting bitmap data from an NSImage in python

开发者 https://www.devze.com 2023-02-12 14:13 出处:网络
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

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 initi开发者_如何转开发alize 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()


Try using:

bitmap = wx.BitmapFromBuffer(...)

instead of ImageFromStream.


Answering my own question: interface to NSIimage is inherently slow, the only workable solution is to avoid it altogether.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号