Skip to content

Commit 83e9834

Browse files
committed
3rd GPT-4.1 attempt
'The "pixmap = surface.peekPixels()" line is wrong. Can you fix that?' Certainly! The correct way to get the pixel buffer from a skia.Surface in skia-python for use with wx.Bitmap.FromBufferRGBA is as follows: Use surface.readPixels() to obtain a numpy array. The numpy array will have shape (height, width, 4), dtype uint8, and pixel order RGBA. Use .tobytes() on the numpy array to obtain the buffer for wx.Bitmap.FromBufferRGBA(width, height, ...). Here is a corrected version of the OnPaint method: ... Summary of fix: Use arr = surface.readPixels() Then wx.Bitmap.FromBufferRGBA(w, h, arr.tobytes()) Do not use .peekPixels() for this purpose in skia-python.
1 parent 5b05e39 commit 83e9834

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

SkiaWxExample-v2.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,13 @@ def OnPaint(self, event):
105105
# Flush drawing
106106
canvas.flush()
107107

108-
# Extract pixel data safely using peekPixels
109-
pixmap = surface.peekPixels()
110-
if pixmap is not None:
111-
buffer = memoryview(pixmap.addr()).tobytes() # Get raw RGBA bytes
112-
wx_bmp = wx.Bitmap.FromBufferRGBA(w, h, buffer)
108+
# Correct pixel extraction for wx.Bitmap
109+
arr = surface.readPixels()
110+
if arr is not None:
111+
wx_bmp = wx.Bitmap.FromBufferRGBA(w, h, arr.tobytes())
113112
dc = wx.AutoBufferedPaintDC(self)
114113
dc.DrawBitmap(wx_bmp, 0, 0)
115114
else:
116-
# Fallback: blank
117115
dc = wx.AutoBufferedPaintDC(self)
118116
dc.Clear()
119117

0 commit comments

Comments
 (0)