]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/RawBitmapAccess.py
4 #----------------------------------------------------------------------
6 class TestPanel(wx
.Panel
):
7 def __init__(self
, parent
, log
):
9 wx
.Panel
.__init
__(self
, parent
, -1)
10 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
12 self
.redBmp
= self
.MakeBitmap(188, 143, 234)
13 self
.greenBmp
= self
.MakeBitmap(35, 142, 35)
14 self
.blueBmp
= self
.MakeBitmap(50, 153, 204)
16 def MakeBitmap(self
, red
, green
, blue
, alpha
=128):
17 bmp
= wx
.EmptyBitmap(100, 100, 32)
19 # Create an object that facilitates access to the bitmap's
21 pixelData
= wx
.AlphaPixelData(bmp
)
23 raise RuntimeError("Failed to gain raw access to bitmap data.")
25 # We have two ways to access each pixel, first we'll use an
26 # iterator to set every pixel to the colour and alpha values
28 for pixel
in pixelData
:
29 pixel
.Set(red
, green
, blue
, alpha
)
31 # Next we'll use the pixel accessor to draw a border
32 pixels
= pixelData
.GetPixels()
34 pixels
.MoveTo(pixelData
, x
, 0)
35 pixels
.Set(red
, green
, blue
, wx
.ALPHA_OPAQUE
)
36 pixels
.MoveTo(pixelData
, x
, 99)
37 pixels
.Set(red
, green
, blue
, wx
.ALPHA_OPAQUE
)
39 pixels
.MoveTo(pixelData
, 0, y
)
40 pixels
.Set(red
, green
, blue
, wx
.ALPHA_OPAQUE
)
41 pixels
.MoveTo(pixelData
, 99, y
)
42 pixels
.Set(red
, green
, blue
, wx
.ALPHA_OPAQUE
)
47 def OnPaint(self
, evt
):
49 dc
.DrawBitmap(self
.redBmp
, 50, 50, True)
50 dc
.DrawBitmap(self
.greenBmp
, 110, 110, True)
51 dc
.DrawBitmap(self
.blueBmp
, 170, 50, True)
53 #----------------------------------------------------------------------
55 def runTest(frame
, nb
, log
):
56 win
= TestPanel(nb
, log
)
59 #----------------------------------------------------------------------
63 overview
= """<html><body>
64 <h2><center>Raw Bitmap Access</center></h2>
66 wx.NativePixelData and wx.AlphaPixelData provide a cross-platform way
67 to access the platform-specific pixel buffer within a wx.Bitmap. They
68 provide both a random access method, and an iterator interface.
75 if __name__
== '__main__':
78 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])