yes, I think you do need to Blit from the ScreenDC to a bitmap -- you need to get it into regular memory some how -- I don't think the screen buffer is in regular memory that you can use for a buffer, etc.
However, how did the image get on the screen in the first place? Maybe you can draw to an off-screen buffer instead, and draw that to the screen to display, but keep it around for other purposes (see double buffering)
you can improve a little bit the numpy code:
mybuffer = numpy.empty((screenSizeX*screenSizeY*3), dtype=numpy.uint8)
# no need to initialize to zeros -- though I doubt you'll see the difference. but you may want to keep the numpy array a shape that makes sense:
mybuffer = numpy.empty((screenSizeX,screenSizeY,3), dtype=numpy.uint8)
now you can get at certain pixels this way:
mybuffer[x_pixel, y_pixel, :]
or get a row with:
mybuffer[:, y_pixel, :]
IMPORTANT NOTE: I think numpy's indexing convention may be (height, width) rather than (width, height) -- so do some experiments to be sure.
More numpy info:
Another nifty feature of numpy is "views" these allow you to make multiple numpy arrays that all share the same data buffer:
In [48]: # first some arbitrary data:
In [49]: a = np.arange(w*h*3, dtype=np.uint8)
In [50]: a_2d = a.reshape((w,h,3))
In [51]: # set a pixel:
In [52]: a_2d[0,0,:] = (0,0,0)
In [53]: # the original array is also changed:
In [54]: a[0]
Out[54]: 0
In [55]: a[0:3]
Out[55]: array([0, 0, 0], dtype=uint8)
In [27]: # set a row white:
In [28]: a_2d[:, 3, :] = (255,255,255)
In [29]: # create a "custom dtype" to represent an RGB pixel:
In [35]: rgb_dt = np.dtype([('R',np.uint8),('G',np.uint8),('B',np.uint8),])
In [36]: rgb_dt
Out[36]: dtype([('R', '|u1'), ('G', '|u1'), ('B', '|u1')])
# create a view with that dtype:
In [40]: a_rgb = a.view(dtype = rgb_dt).reshape((w,h))
In [41]: a_rgb.shape
Out[41]: (5, 7)
In [42]: # now set a pixel white:
In [43]: a_rgb[3,4] = (255, 255, 255)
In [44]: a_rgb
Out[44]:
array([[(0, 0, 0), (3, 4, 5), (6, 7, 8), (255, 255, 255), (12, 13, 14),
(15, 16, 17), (18, 19, 20)],
[(21, 22, 23), (24, 25, 26), (27, 28, 29), (255, 255, 255),
(33, 34, 35), (36, 37, 38), (39, 40, 41)],
[(42, 43, 44), (45, 46, 47), (48, 49, 50), (255, 255, 255),
(54, 55, 56), (57, 58, 59), (60, 61, 62)],
[(63, 64, 65), (66, 67, 68), (69, 70, 71), (255, 255, 255),
(255, 255, 255), (78, 79, 80), (81, 82, 83)],
[(84, 85, 86), (87, 88, 89), (90, 91, 92), (255, 255, 255),
(96, 97, 98), (99, 100, 101), (102, 103, 104)]],
dtype=[('R', '|u1'), ('G', '|u1'), ('B', '|u1')])
In [46]: # get just the green:
In [47]: a_rgb['G']
Out[47]:
array([[ 0, 4, 7, 255, 13, 16, 19],
[ 22, 25, 28, 255, 34, 37, 40],
[ 43, 46, 49, 255, 55, 58, 61],
[ 64, 67, 70, 255, 255, 79, 82],
[ 85, 88, 91, 255, 97, 100, 103]], dtype=uint8)
# And lots more nifty operations!
Sorry to get carried away -- I am a bit of a numpy evangelist!
-Chris
···
On 11/5/11 7:49 AM, andrea console wrote:
Finally this is the code that do exactly what I want:
screenSizeX, screenSizeY = wx.DisplaySize()
bitmap=wx.EmptyBitmap(screenSizeX ,screenSizeY,-1)
memory=wx.MemoryDC()
memory.SelectObject(bitmap)
memory.Blit(0,0,screenSizeX,screenSizeY,dcScreen,0,0)
mybuffer = numpy.zeros((screenSizeX*screenSizeY*3), dtype=numpy.uint8)
bitmap.CopyToBuffer(mybuffer, wx.BitmapBufferFormat_RGB)
print mybuffer[3*(actualX+screenSizeX*actualY)],
mybuffer[3*(actualX+screenSizeX*actualY)+1],
mybuffer[3*(actualX+screenSizeX*actualY)+2]
Just a final question: is this the most efficient option? I mean: do I
really need to "Blit" to a bitmap or I can use a more straightforward
way?
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov