-1

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()
Dmitry Chichkov
  • 602
  • 5
  • 12
  • The question was about "extracting the data from *NSImage*" - as per the title! The rest of the code is there just to have a complete/working example. – Dmitry Chichkov Oct 06 '18 at 02:25

2 Answers2

2

Try using:

bitmap = wx.BitmapFromBuffer(...)

instead of ImageFromStream.

kenorb
  • 155,785
  • 88
  • 678
  • 743
AWainb
  • 868
  • 2
  • 13
  • 27
1

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

Dmitry Chichkov
  • 602
  • 5
  • 12