def GetHitTestColor(self, xy): """ Get the hit test colour :param `xy`: the position to get the hit test colour for """ if self._ForegroundHTBitmap: pdata = wx.AlphaPixelData(self._ForegroundHTBitmap) else: pdata = wx.AlphaPixelData(self._HTBitmap) if not pdata: raise RuntimeError("Trouble Accessing Hit Test bitmap") pacc = pdata.GetPixels() pacc.MoveTo(pdata, xy[0], xy[1]) return pacc.Get()[:3]
def process_pick_points(self, x, y): context = wx.ClientDC(self) w, h = self.GetClientSize() if wx.VERSION[0] > 3: bitmap = wx.Bitmap(w, h, -1) else: # https://discuss.wxpython.org/t/wx-bitmap-x-y-and-wx-bitmap-create-x-y-vs-wx-emptybitmap-x-y/29463 bitmap = wx.EmptyBitmap(w, h, -1) memory = wx.MemoryDC(bitmap) memory.SelectObject(bitmap) memory.Blit(0, 0, w, h, context, 0, 0) memory.SelectObject(wx.NullBitmap) if (wx.Platform == '__WXMAC__'): pixelData = wx.AlphaPixelData(bitmap) pixelAccessor = pixelData.GetPixels() pixelAccessor.MoveTo(pixelData, x, y) c = pixelAccessor.Get() else: c = memory.GetPixel(x, y) bg = self.GetBackgroundColour() self._clicked = None min_dist = sys.maxsize closest_hkl = None for k, (x2, y2) in enumerate(self._points_2d): dist = sqrt((x2 - x)**2 + (y2 - y)**2) if (dist <= (self._radii_2d[k] + 2)): self._clicked = k break hkl = d_min = value = None if (self._clicked is not None): self.GetParent().update_clicked(self._clicked) self.Refresh()
def MakeBitmap(red, green, blue, alpha=128): # Create the bitmap that we will stuff pixel values into using # the raw bitmap access classes. bmp = wx.Bitmap(16, 16, 32) # Create an object that facilitates access to the bitmap's # pixel buffer pixelData = wx.AlphaPixelData(bmp) if not pixelData: raise RuntimeError("Failed to gain raw access to bitmap data.") # We have two ways to access each pixel, first we'll use an # iterator to set every pixel to the colour and alpha values # passed in. for pixel in pixelData: pixel.Set(red, green, blue, alpha) # Next we'll use the pixel accessor to set the border pixels # to be fully opaque pixels = pixelData.GetPixels() for x in six.moves.range(16): pixels.MoveTo(pixelData, x, 0) pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) pixels.MoveTo(pixelData, x, 16 - 1) pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) for y in six.moves.range(16): pixels.MoveTo(pixelData, 0, y) pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) pixels.MoveTo(pixelData, 16 - 1, y) pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) return bmp
def __init__(self, *args, **kw): wx.Panel.__init__(self, *args, **kw) self.Bind(wx.EVT_PAINT, self.OnPaint) self.SetBackgroundColour("white") # maskless image self.bmp1 = getTest2Bitmap() # give this one a mask self.bmp2 = getTest2Bitmap() self.bmp2.SetMaskColour("blue") # now make one in which the opaque pixels from #2 are made # partially transparent bmp = getTest2Bitmap() bmp.SetMaskColour("blue") img = bmp.ConvertToImage() img.InitAlpha() self.bmp3 = img.ConvertToBitmap() pixelData = wx.AlphaPixelData(self.bmp3) if not pixelData: raise RuntimeError("Failed to gain raw access to bitmap data.") for pixel in pixelData: red, green, blue, alpha = pixel.Get() if alpha == 255: pixel.Set(red, green, blue, 128)
def MakeMask(self, bitmap, alpha=32): pixelData = wx.AlphaPixelData(bitmap) pixels = pixelData.GetPixels() for y in range(28): pixels.MoveTo(pixelData, 0, y) for x in range(28): pixels.Set(255, 0, 0, alpha) pixels.nextPixel() return bitmap
def OnInit(self): #frame = TestFrame() #frame.Show(True) #Start, Stop, Step = (0, 200, 10) Start, Stop, Step = (0, 200, 20) Depth = 24 B = wx.EmptyBitmap(Size[0], Size[1], depth=24) #B = wx.EmptyBitmap(Size[0], Size[1]) print "Bitmap depth is:", B.GetDepth() #raise Exception("stopping") dc = wx.MemoryDC() dc.SelectObject(B) dc.SetBackground(wx.BLACK_BRUSH) dc.Clear() del dc i = 0 j = 0 for r in range(Start, Stop, Step): for g in range(Start, Stop, Step): for b in range(Start, Stop, Step): i += 1 if i >= Size[0]: i = 0 j += 1 if j >= Size[1]: raise Exception("Too many color tests for bitmap size") inColor = (r, g, b) print "Drawing:", inColor, "To pixel:", (i, j) dc = wx.MemoryDC(B) dc.SetPen(wx.Pen(wx.Color(*inColor), 4)) dc.DrawPoint(i, j) del dc print "Bitmap depth is:", B.GetDepth() pdata = wx.AlphaPixelData(B) pacc = pdata.GetPixels() pacc.MoveTo(pdata, i, j) outColor = pacc.Get()[:3] print "Got", outColor if inColor != outColor: print "Error!!, inColor = %s, outColor = %s at pixel: %s" % ( inColor, outColor, (i, j)) B.SaveFile("junk.png", wx.BITMAP_TYPE_PNG) return True
def MakeBitmap(self, red, green, blue, alpha=128): # Create the bitmap that we will stuff pixel values into using # the raw bitmap access classes. bmp = wx.EmptyBitmap(DIM, DIM, 32) # Create an object that facilitates access to the bitmap's # pixel buffer pixelData = wx.AlphaPixelData(bmp) if not pixelData: raise RuntimeError("Failed to gain raw access to bitmap data.") # We have two ways to access each pixel, first we'll use an # iterator to set every pixel to the colour and alpha values # passed in. for pixel in pixelData: pixel.Set(red, green, blue, alpha) # This block of code is another way to do the same as above, # but with the accessor interface instead of the Python # iterator. It is a bit faster than the above because it # avoids the iterator/generator magic, but it is not nearly as # 'clean' looking ;-) #pixels = pixelData.GetPixels() #for y in xrange(DIM): # for x in xrange(DIM): # pixels.Set(red, green, blue, alpha) # pixels.nextPixel() # pixels.MoveTo(pixelData, 0, y) # Next we'll use the pixel accessor to set the border pixels # to be fully opaque pixels = pixelData.GetPixels() for x in xrange(DIM): pixels.MoveTo(pixelData, x, 0) pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) pixels.MoveTo(pixelData, x, DIM-1) pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) for y in xrange(DIM): pixels.MoveTo(pixelData, 0, y) pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) pixels.MoveTo(pixelData, DIM-1, y) pixels.Set(red, green, blue, wx.ALPHA_OPAQUE) return bmp
def getData(self, file_format): ''' Returns the picture of this surface as a string with file_format. where file format can be something like 'png', 'jpg' or 'raw' or any other kind of supported image format. ''' if file_format == 'raw': # bit faster than the raw2 method because we use array.array. # Should probably use a numpy array instead. noPixels = self.bitmap.Size[0] * self.bitmap.Size[1] if self.hasAlpha: result = array.array('c', '\0' * 4 * noPixels) self.bitmap.CopyToBuffer(result, wx.BitmapBufferFormat_RGBA) #self.bitmap.CopyToBuffer( result, wx.BitmapBufferFormat_ARGB32 ) return result.tostring() else: result = array.array('c', '\0' * 3 * noPixels) self.bitmap.CopyToBuffer(result, wx.BitmapBufferFormat_RGB) return result.tostring() elif file_format == 'raw2': # very slow, because we iterate over each pixel result = '' for it in wx.AlphaPixelData(self.bitmap): pixel = it.Get() result += chr(pixel[0]) + chr(pixel[1]) + chr(pixel[2]) if self.bitmap.HasAlpha(): result += chr(pixel[3]) return result else: img = self.bitmap.ConvertToImage() import cStringIO outputStream = cStringIO.StringIO() if file_format == 'jpg': file_format = 'jpeg' img.SaveStream(wx.OutputStream(outputStream), ConstantTable.getEnum('bitmap_type', file_format)) data = outputStream.getvalue() outputStream.close() return data
def test_rawbmp1(self): DIM = 100 red = 10 green = 20 blue = 30 alpha = 128 bmp = wx.Bitmap(DIM, DIM, 32) pixelData = wx.AlphaPixelData(bmp) self.assertTrue(pixelData) # Test using the __iter__ generator for pixel in pixelData: pixel.Set(red, green, blue, alpha) # This block of code is another way to do the same as above pixels = pixelData.GetPixels() for y in range(DIM): pixels.MoveTo(pixelData, 0, y) for x in range(DIM): pixels.Set(red, green, blue, alpha) pixels.nextPixel()
def OnLeftClick(self, evt): if self.shoot is not None: self.shoot.Play(wx.SOUND_ASYNC) dc = wx.ClientDC(self) dc.SetPen(wx.Pen(self.color)) dc.SetBrush(wx.Brush(self.color)) pos = evt.GetPosition() dc.DrawCircle(pos.x, pos.y, 3) self.time = time.time() self.click += 1 points = 0 if (pos.x > self.x and pos.x < self.x + self.w and pos.y > self.y and pos.y < self.y + self.h): pixelData = wx.AlphaPixelData(self.bitmap_mask) pixelAccessor = pixelData.GetPixels() pixelAccessor.MoveTo(pixelData, pos.x - self.x, pos.y - self.y) r, g, b, a = pixelAccessor.Get() points = int(round(r / 24.0)) self.score += points self.shoots.append( (pos.x / float(self.Width), pos.y / float(self.Height), points)) self.win.frame.SetTitle('Diana %s disparos - %s puntos' % (self.click, self.score))
def test_pixel(self, args): _ = wx.App() # Need to create an App instance before doing anything x = (int)(args[0]) y = (int)(args[1]) x = (int)(x * 2.6) y = (int)(y * 2.6) screen = wx.ScreenDC() screen_size = screen.GetSize() scren_size_x = screen_size[1] / 2 scren_size_y = screen_size[0] / 2 bmp = self.get_bmp() apd = wx.AlphaPixelData(bmp, wx.Rect(0, 0, scren_size_x, scren_size_y)) pixels = apd.GetPixels() pixels.MoveTo(apd, x, y) pixel = pixels.Get() print(f"pixel ({x},{y}): {pixel}") bmp.SaveFile('screenshot.png', wx.BITMAP_TYPE_PNG)