def from_data(cls, data, size): """ Construct an image from a bytearray of RGBA bytes. Parameters ---------- data : bytearray A bytearray of the image data in RGBA32 format. size : (width, height) The width, height size tuple of the image. Returns ------- results : AbstractTkImage An appropriate image instance. """ w, h = size # Split the array into color and alpha to satisfy WX rgb_array = bytearray(w*h*3) alpha_array = bytearray(w*h) rgb_array[0::3] = data[0::4] rgb_array[1::3] = data[1::4] rgb_array[2::3] = data[2::4] alpha_array[:] = data[3::4] return cls(wx.ImageFromDataWithAlpha(w, h, rgb_array, alpha_array))
def __new__(cls, *args, **kwargs): if isinstance(args[0], (file, io.IOBase)): return wx.ImageFromStream(*args) elif len(args) >= 2 and isinstance(args[0], int) and isinstance(args[1], int): if "data" in kwargs and "alpha" in kwargs: return wx.ImageFromDataWithAlpha(*args, **kwargs) else: return wx.EmptyImage(*args, **kwargs) return super(ImageClever, cls).__new__(cls, *args, **kwargs)
def makeBitmap(tex, alpha = True, thumb = False): """Returns a wx.Bitmap. 'tex' should be a Panda Texture object.""" assert isinstance(tex, Texture) xsize, ysize = tex.getXSize(), tex.getYSize() data, adata = None, None # Only available in 1.6.0 and higher if hasattr(Texture, "getRamImageAs"): data = tex.getRamImageAs("RGB") if alpha and tex.getNumComponents() in [2, 4]: adata = tex.getRamImageAs("A") else: # Grab the RGB data ttex = tex.makeCopy() ttex.setFormat(Texture.FRgb) if hasattr(ttex, "getUncompressedRamImage"): data = ttex.getUncompressedRamImage() else: data = ttex.getRamImage() # If we have an alpha channel, grab it as well. if alpha and tex.getNumComponents() in [2, 4]: ttex = tex.makeCopy() ttex.setFormat(Texture.FAlpha) if hasattr(ttex, "getUncompressedRamImage"): adata = ttex.getUncompressedRamImage() else: adata = ttex.getRamImage() # Now for the conversion to wx. assert not data.isNull() if adata == None: img = wx.ImageFromData(xsize, ysize, data.getData()) else: assert not adata.isNull() img = wx.ImageFromDataWithAlpha(xsize, ysize, data.getData(), adata.getData()) if thumb: # Resize it not to be bigger than the THUMBNAIL_SIZE. if xsize == ysize: xsize, ysize = THUMBNAIL_SIZE, THUMBNAIL_SIZE else: factor = ysize / float(THUMBNAIL_SIZE) xsize, ysize = int(xsize / factor), int(ysize / factor) if xsize > THUMBNAIL_MAX_WIDTH: factor = xsize / float(THUMBNAIL_MAX_WIDTH) xsize, ysize = int(xsize / factor), int(ysize / factor) img.Rescale(xsize, ysize, wx.IMAGE_QUALITY_HIGH) # Swap red and blue channels, if we aren't using 1.6.0 or higher. if not hasattr(Texture, "getRamImageAs"): for x in xrange(xsize): for y in xrange(ysize): img.SetRGB(x, y, img.GetBlue(x, y), img.GetGreen(x, y), img.GetRed(x, y)) img = img.Mirror(False) # Downside up. return wx.BitmapFromImage(img)
def Copy(self): ss = self._brain.screenshot('rgba', True) ss = np.round(ss * 255).astype(np.uint8) h, w, _ = ss.shape image = wx.ImageFromDataWithAlpha(w, h, ss[:, :, :3].tostring(), ss[:, :, 3].tostring()) bitmap = image.ConvertToBitmap() data = wx.BitmapDataObject(bitmap) if not wx.TheClipboard.Open(): getLogger('eelbrain').debug("Failed to open clipboard") return try: wx.TheClipboard.SetData(data) finally: wx.TheClipboard.Close() wx.TheClipboard.Flush()
def NDImage2wxImage(image): """ Converts a NDImage into a wxImage. Note, the copy of the data will be avoided whenever possible. image (ndarray of uint8 with shape YX3 or YX4): original image, order of last dimension is RGB(A) return (wxImage) """ assert (len(image.shape) == 3) size = image.shape[1::-1] if image.shape[2] == 3: # RGB wim = wx.ImageFromBuffer(*size, dataBuffer=image) # 0 copy return wim elif image.shape[2] == 4: # RGBA # 2 copies return wx.ImageFromDataWithAlpha( *size, data=numpy.ascontiguousarray(image[:, :, 0:3]), alpha=numpy.ascontiguousarray(image[:, :, 3])) else: raise ValueError("image is of shape %s" % (image.shape, ))
def BitmapRGBA_handler(self, texel): w, h = texel.size im = wx.ImageFromDataWithAlpha(w, h, texel.data, texel.alpha) bitmap = wx.BitmapFromImage(im) return [BitmapBox(bitmap, device=self.device)]