def showImgProcRslt(self, fp=''): """ Show an image file and its result after image processing Args: fp (str): File path of image to show Returns: None """ if DEBUG: print("ImgProcsFrame.showImgProcRslt()") if fp == '': fp = self.fileList[0] iImg = np.array(Image.open(fp)) oImg = self.procImg(iImg.copy()) ### draw image for i in range(2): if i == 0: img = iImg k = "ip" elif i == 1: img = oImg k = "op" if len(img.shape) > 2 and img.shape[2] == 4: wxImg = wx.ImageFromBuffer(img.shape[1], img.shape[0], img[:,:,:3].tostring(), img[:,:,3].tostring()) else: wxImg = wx.ImageFromBuffer(img.shape[1], img.shape[0], img.tostring()) w = wx.FindWindowByName("%s_sBmp"%(k), self.panel[k]) w.SetBitmap(wx.Bitmap(wxImg)) self.panel[k].SetupScrolling()
def load_image(self): filepath = self.photoTxt.GetValue() image = io.imread(filepath) height, width, nrgb = image.shape origin_image = wx.ImageFromBuffer(width, height, image) image = image_process.segmentation(image) height, width, nrgb = image.shape wximg = wx.ImageFromBuffer(width, height, np.array(image)) image_level_Set = image_process.segmentation_level_Set(image) height, width, nrgb = image_level_Set.shape wximg_level_Set = wx.ImageFromBuffer(width, height, np.array(image_level_Set)) self.image = wximg return origin_image, wximg, wximg_level_Set
def set_scale(self, scale): """Scale a numpy array by an integer factor This turns out to be too slow to be used by the screen display. OpenGL displays don't use this at all because the display hardware scales automatically. """ self.screen_scale = scale h = self.emulator.height w = self.emulator.width if scale > 1: self.scaled_frame = np.empty((h * scale, w * scale, 3), dtype=np.uint8) self.image = wx.ImageFromBuffer(w * scale, h * scale, self.scaled_frame) else: self.scaled_frame = None self.image = wx.ImageFromBuffer(w, h, self.emulator.screen_rgb)
def updateArray(self, array): """ Update the SLM monitor with the supplied array. Note that the array is not the same size as the SLM resolution, the image will be deformed to fit the screen. """ #create a wx.Image from the array h, w = array.shape[0], array.shape[1] if len(array.shape) == 2: bw_array = array.copy() bw_array.shape = h, w, 1 color_array = np.concatenate((bw_array, bw_array, bw_array), axis=2) data = color_array.tostring() else: data = array.tostring() img = wx.ImageFromBuffer(width=w, height=h, dataBuffer=data) # Create the event event = ImageEvent() event.img = img event.eventLock = self.eventLock # Wait for the lock to be released (if isImageLock = True) # to be sure that the previous image has been displayed # before displaying the next one - it avoids skipping images if (self.isImageLock): event.eventLock.acquire() # Wait (can bug when closing/opening the SLM too quickly otherwise) time.sleep(0.5) # Trigger the event (update image) self.vt.frame.AddPendingEvent(event)
def show(self, colormapper, progress=None): if not self.cube: return try: # remove old image for memory management purposes del self.image self.image = None refresh=False if self.indexes: if len(self.indexes)!=len(self.bands): refresh=True else: for i in range(len(self.indexes)): if self.indexes[i]!=self.bands[i][0]: refresh=True break if refresh or not self.bands: self.loadBands() self.processFilters(progress) rgb = colormapper.getRGB(self.height, self.width, self.planes) # image uses the rgb data and doesn't create a new copy self.image = wx.ImageFromBuffer(self.width, self.height, rgb) # self.Refresh() except Exception, e: import traceback dprint(traceback.format_exc()) Publisher().sendMessage('peppy.log.error', traceback.format_exc()) # recreate empty image if necessary self.initBitmap()
def onPaintSPT(self, event): """ Draw spectrogram of template WAV data. Args: event (wx.Event) Returns: None """ if DEBUG: print("PyListenerFrame.onPaintSPT()") evtObj = event.GetEventObject() eoName = evtObj.GetName() dc = wx.PaintDC(evtObj) dc.SetBackground(wx.Brush('#333333')) dc.Clear() ### draw spectrogram ad = self.pl.tSpAD imgArr = np.stack( (ad, ad, ad), axis=2 ) img = wx.ImageFromBuffer(imgArr.shape[1], imgArr.shape[0], imgArr) bmp = wx.Bitmap(img) # wx.BitmapFromImage(img) dc.DrawBitmap(bmp, 0, 0) ### draw additional notations if self.pl.templP != None: # analyzed parameters are available p = self.pl.templP lx = 0; rx = ad.shape[1] cmX = p["centerOfMassX"] cmY = p["centerOfMassY"] if rx > 0: self.drawParamsOfSound(dc, p, lx, rx, cmX, cmY) event.Skip()
def numpyToWxImage(self, a): ''' Converts a numpy array to a wx.Image. ''' # note the use of the ascontiguousarray method, otherwise it may break for big arrays return wx.ImageFromBuffer(a.shape[0], a.shape[1], np.ascontiguousarray(a))
def update_result_image_panel(self, event: wx.Event): current_pose = self.get_current_pose() if self.last_pose is not None \ and self.last_pose == current_pose \ and self.last_output_index == self.output_index_choice.GetSelection(): return self.last_pose = current_pose self.last_output_index = self.output_index_choice.GetSelection() if self.torch_source_image is None: self.draw_source_image_string(self.result_image_panel, use_paint_dc=False) return pose = torch.tensor(current_pose, device=self.device) output_index = self.output_index_choice.GetSelection() output_image = self.poser.pose(self.torch_source_image, pose, output_index)[0].detach().cpu() numpy_image = numpy.uint8( numpy.rint( convert_output_image_from_torch_to_numpy(output_image) * 255.0)) self.last_output_numpy_image = numpy_image wx_image = wx.ImageFromBuffer(numpy_image.shape[0], numpy_image.shape[1], numpy_image[:, :, 0:3].tobytes(), numpy_image[:, :, 3].tobytes()) wx_bitmap = wx_image.ConvertToBitmap() dc = wx.ClientDC(self.result_image_panel) dc.Clear() dc.DrawBitmap(wx_bitmap, (256 - numpy_image.shape[0]) // 2, (256 - numpy_image.shape[1]) // 2, True)
def _init_canvas(self): w, h = self._size self._array = np.zeros((h, w, 4), dtype=np.uint8) self._cv_image = converters.np_rgba_to_vtk(self._array) self.mapper = vtk.vtkImageMapper() self.mapper.SetInputData(self._cv_image) self.mapper.SetColorWindow(255) self.mapper.SetColorLevel(128) self.actor = vtk.vtkActor2D() self.actor.SetPosition(0, 0) self.actor.SetMapper(self.mapper) self.actor.GetProperty().SetOpacity(0.99) self.canvas_renderer.AddActor2D(self.actor) self.rgb = np.zeros((h, w, 3), dtype=np.uint8) self.alpha = np.zeros((h, w, 1), dtype=np.uint8) self.bitmap = wx.Bitmap.FromRGBA(w, h) try: self.image = wx.Image(w, h, self.rgb, self.alpha) except TypeError: self.image = wx.ImageFromBuffer(w, h, self.rgb, self.alpha)
def add_lightness_for_wxbmp(wxbmp, lightness, maskpos=(0, 0)): """ 通常時のボタン画像からdisabled用の画像を作る。 RGB値の範囲を 0~255 から min~max に変更する。 wxbmp: wx.Bitmap """ try: func = _imageretouch.add_lightness except: func = _add_lightness wximg = wxbmp.ConvertToImage().ConvertToGreyscale() buf = str(wximg.GetDataBuffer()) alphabuf = wximg.GetAlphaBuffer() buf = bytearray(buf) w = wximg.GetWidth() h = wximg.GetHeight() func(buf, (w, h), lightness) wximg = wx.ImageFromBuffer(w, h, buffer(buf), alphaBuffer=alphabuf) wxbmp = wx.BitmapFromImage(wximg) x, y = maskpos wxbmp.SetMaskColour( (wximg.GetRed(x, y), wximg.GetGreen(x, y), wximg.GetBlue(x, y))) return wxbmp
def run(self): while self.__running: if self.__start: if self.__cap.isOpened(): ret, frame = self.__cap.read() if ret: if self.__enable: frame_res = processing_image(frame) else: frame_res = frame if self.__save: self.__out.write(frame_res) else: pass height, width = frame_res.shape[:2] frame_res = cv2.cvtColor(frame_res, cv2.COLOR_BGR2RGB) bmp = wx.ImageFromBuffer(width, height, frame_res) wx.PostEvent(self.__gui, ResultEvent(Events.EVT_DISPLAY_ID, bmp)) else: self.Stop() else: pass time.sleep(1 / int(self.__fps)) else: time.sleep(0.1)
def wximg(self): """ @brief Returns (after constructing, if necessary) a wx.Image representation of this Image. """ if self._wx is None: img = self.copy(channels=3, depth=8) self._wx = wx.ImageFromBuffer(img.width, img.height, img.array) return self._wx
def updateArray(self, ev_mask): """Update the SLM monitor with the supplied EvMask. Note that the array is not the same size as the SLM resolution, the image will be deformed to fit the screen.""" mask = ev_mask.correc + ev_mask array = mask.image() # Padding: Like they do in the software pad = np.zeros((600, 8), dtype=np.uint8) array = np.append(array, pad, 1) # create a wx.Image from the array h, w = array.shape[0], array.shape[1] if len(array.shape) == 2: bw_array = array.copy() bw_array.shape = h, w, 1 color_array = np.concatenate((bw_array, bw_array, bw_array), axis=2) data = color_array.tostring() else: data = array.tostring() img = wx.ImageFromBuffer(width=w, height=h, dataBuffer=data) # Create the event event = ImageEvent() event.img = img event.eventLock = self.eventLock # Wait for the lock to be resleased (if isImageLock = True) # to be sure that the previous is image has been displayed # before displaying the next one - avoids skipping inmages if (self.isImageLock): event.eventLock.acquire() # Trigger the event (update image) self.vt.frame.AddPendingEvent(event)
def run(self): while self.__running: if self.__start: try: img = cv2.imread(self.__file_path) if self.__enable: img_res = processing_image(img) else: img_res = img if self.__save: self._save_image(self._get_save_path(self.__file_path), img_res) else: pass height, width = img_res.shape[:2] img_res = cv2.cvtColor(img_res, cv2.COLOR_BGR2RGB) bmp = wx.ImageFromBuffer(width, height, img_res) wx.PostEvent(self.__gui, ResultEvent(Events.EVT_DISPLAY_ID, bmp)) except Exception: pass self.Stop() else: pass time.sleep(0.1)
def GetData(self, event): Num = 0 while self.handle.more_data(): iter = 0 q = [] pos, n, q = self.handle.read(1000, q) for p in q: if iter == 0: temp = p.T[::-1] vector = np.log10(temp + 1) else: temp = p.T[::-1] vector = np.append(vector, np.log10(temp + 1), axis=1) iter = iter + 1 #plt.imshow(np.log(p.T+1)) #plt.show() vector = vector / np.amax(vector) vector = [vector, vector, vector] vector = np.dstack(vector) #plt.imsave(path, vector, cmap = cm.gray) im = wx.ImageFromBuffer(int(np.size(vector, axis=1)), int(np.size(vector, axis=0)), np.uint8(vector)) if Num == 0: self.specW = vector list = [vector] else: self.specW = np.append(self.specW, vector, axis=1) list.append(vector) Num = Num + 1 self.spec = self.specW * 255.0 self.orim = wx.ImageFromBuffer(int(np.size(self.spec, axis=1)), int(np.size(self.spec, axis=0)), np.uint8(self.spec)) self.orim = self.orim.Rescale(self.orim.GetWidth(), 140) self.SpecLen = self.orim.GetWidth() self.ratio = float(self.VideoLen) / self.SpecLen / 1000 self.wind.overlay.Reset() self.wind.SetBitmap(self.orim.ConvertToBitmap()) self.Spec_Flag = 1 self.im = self.orim self.bm = self.im.ConvertToBitmap()
def set_image(self): h, w = self.height_map.shape self.array = np.zeros((h, w, 3), dtype='uint8') self.array[:, :, 0] = self.height_map self.array[:, :, 1] = self.height_map self.array[:, :, 2] = self.height_map print(self.array.shape) image = wx.ImageFromBuffer(w, h, self.array) self.Panel = ImagePanel(image, self)
def wxBitmapFromCvImage(image): image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) h, w = image.shape[:2] wxImage = wx.ImageFromBuffer(w, h, image) if WX_MAJOR_VERSION < 4: bitmap = wx.BitmapFromImage(wxImage) else: bitmap = wx.Bitmap(wxImage) return bitmap
def wxBitmapFromCvImage(image): if len(image.shape) < 3: image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) else: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) h, w = image.shape[:2] wxImage = wx.ImageFromBuffer(w, h, image) bitmap = wx.BitmapFromImage(wxImage) return bitmap
def SetPixels(self, pixels, colormap): if pixels is None: self.raw_image = None self.bitmap = None self.Refresh() else: h, w = pixels.shape[0:2] p = colormap(pixels, bytes=True)[:, :, 0:3] self.raw_image = wx.ImageFromBuffer(w, h, p.tostring()) self.SetZoom(self.zoom, force=True, immediate=True)
def onView(self, a): si = StringIO(a) im = Image.open(si) print im.size[0], im.size[1] img = wx.ImageFromBuffer(im.size[0], im.size[1], im.tobytes()) self.NW = 1180 self.NH = 620 self.imageCtrl.SetBitmap(wx.BitmapFromImage(img)) self.panel.Refresh() #img = img.Scale(1080,720) si.close()
def on_paint(self, evt): with self.mutex: if self.projection is not None: rgb8_projection = self.bridge.imgmsg_to_cv( self.projection, "rgb8") image = wx.ImageFromBuffer(rgb8_projection.width, rgb8_projection.height, rgb8_projection.tostring()) bitmap = image.ConvertToBitmap() dc = wx.BufferedPaintDC(self.panel, bitmap) self.paint_flag.set()
def PlotImage(self, image=None): if image is None: image = self.GetImage() width, height = image.size mybuffer = image.convert('RGB').tobytes() wximg = wx.ImageFromBuffer(width, height, mybuffer) # Image scaling wximg = wximg.Scale(width*2, height*2) self.img.Destroy() self.img = wx.BitmapFromImage(wximg) self.imageCtrl.SetBitmap(self.img)
def load_img(self, fn): """加载图片;""" if isinstance(fn, str): img = Image.open(fn) img = img.resize((256, 256)) elif isinstance(fn, Image.Image): img = fn width, height = img.size img = img.tobytes() arr = np.array(img) bmp = wx.ImageFromBuffer(width, height, arr).ConvertToBitmap() return bmp
def UpdateView(self): xform = self.parent.ActiveXform for name in self.sliders: self.UpdateSlider(name, getattr(xform, name)) self.animflag.SetValue(xform.animate) buff = self.parent.flame.gradient.to_buffer() color = min(max(int(xform.color * 256), 1), 255) img = wx.ImageFromBuffer(color, 1, buff[:color * 3]) img.Rescale(96, 28) self.bmp = wx.BitmapFromImage(img) img = wx.ImageFromBuffer(1, 1, buff[color * 3:color * 3 + 3]) img.Rescale(12, 32) self.bmp2 = wx.BitmapFromImage(img) img = wx.ImageFromBuffer(256 - color, 1, buff[color * 3:]) img.Rescale(96, 28) self.bmp3 = wx.BitmapFromImage(img) self.Refresh()
def sliderUpdate2(self, event): self.pos = self.sld1.GetValue() self.wind.Refresh() if self.Spec_Flag == 1: self.wind.overlay.Reset() self.spec = self.specW * self.pos self.im = wx.ImageFromBuffer(int(np.size(self.spec, axis=1)), int(np.size(self.spec, axis=0)), np.uint8(self.spec)) self.im = self.im.Rescale(self.im.GetWidth(), 140) self.wind.SetBitmap(self.im.ConvertToBitmap()) else: pass
def Update(self, flame=None): flame = flame or self.parent.flame # Make the gradient image img = wx.ImageFromBuffer(256, 1, flame.gradient.to_buffer()) img.Rescale(384, 50) self.bmp = wx.BitmapFromImage(img) # Calculate the color histogram genome = Genome.from_string(flame.to_string(omit_details=True))[0] flam3_colorhist(genome, 3, RandomContext(), self.colorhist_array) self.Refresh()
def __init__(self, title="Micro App"): wx.Frame.__init__(self, None, -1, title) MenuBar = wx.MenuBar() FileMenu = wx.Menu() item = FileMenu.Append(wx.ID_ANY, text="&Open") self.Bind(wx.EVT_MENU, self.OnOpen, item) item = FileMenu.Append(wx.ID_PREFERENCES, text="&Preferences") self.Bind(wx.EVT_MENU, self.OnPrefs, item) item = FileMenu.Append(wx.ID_EXIT, text="&Exit") self.Bind(wx.EVT_MENU, self.OnQuit, item) MenuBar.Append(FileMenu, "&File") HelpMenu = wx.Menu() item = HelpMenu.Append(wx.ID_HELP, "Test &Help", "Help for this simple test") self.Bind(wx.EVT_MENU, self.OnHelp, item) ## this gets put in the App menu on OS-X item = HelpMenu.Append(wx.ID_ABOUT, "&About", "More information About this program") self.Bind(wx.EVT_MENU, self.OnAbout, item) MenuBar.Append(HelpMenu, "&Help") self.SetMenuBar(MenuBar) btn = wx.Button(self, label="NewImage") btn.Bind(wx.EVT_BUTTON, self.OnNewImage) self.Bind(wx.EVT_CLOSE, self.OnQuit) ##Create numpy array, and image from it w = h = 200 self.array = rand.randint(0, 255, (h, w, 3)).astype('uint8') self.array = np.zeros((h, w, 3), dtype='uint8') self.array[:, :, 0] = 128 print(self.array.shape) image = wx.ImageFromBuffer(w, h, self.array) #image = wx.Image("Images/cute_close_up.jpg") self.Panel = ImagePanel(image, self) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(btn, 0, wx.ALIGN_CENTER | wx.ALL, 5) sizer.Add(self.Panel, 1, wx.GROW) self.SetSizer(sizer)
def OnUpdateVidoe(self, event): ret, frame = self.cap.read() if ret: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) img_buf = wx.ImageFromBuffer(frame.shape[1], frame.shape[0], frame) self.bmp = wx.BitmapFromImage(img_buf) # modify this part to update every 10 sec etc... # right now, it's realtime update (every frame) y = frame.mean(axis=0).mean(axis=1) self.line.set_ydata(y) self.graph.canvas.draw() self.Refresh()
def update_result_image_panel(self, event: wx.Event): tic = time.perf_counter() ifacialmocap_pose = self.capture_data.read_data() current_pose = self.pose_converter.convert(ifacialmocap_pose) if self.last_pose is not None \ and self.last_pose == current_pose \ and self.last_output_index == self.output_index_choice.GetSelection(): return self.last_pose = current_pose self.last_output_index = self.output_index_choice.GetSelection() if self.torch_source_image is None: self.draw_source_image_string(self.result_image_panel, use_paint_dc=False) return pose = torch.tensor(current_pose, device=self.device) output_index = self.output_index_choice.GetSelection() output_image = self.poser.pose(self.torch_source_image, pose, output_index)[0].detach().cpu() numpy_image = convert_output_image_from_torch_to_numpy(output_image) background_choice = self.output_background_choice.GetSelection() if background_choice != 0: background = numpy.zeros((numpy_image.shape[0], numpy_image.shape[1], numpy_image.shape[2])) background[:, :, 3] = 1.0 if background_choice == 1: background[:, :, 1] = 1.0 elif background_choice == 2: background[:, :, 2] = 1.0 elif background_choice == 4: background[:, :, 0:3] = 1.0 numpy_image = self.blend_with_background(numpy_image, background) numpy_image = numpy.uint8(numpy.rint(numpy_image * 255.0)) wx_image = wx.ImageFromBuffer( numpy_image.shape[0], numpy_image.shape[1], numpy_image[:, :, 0:3].tobytes(), numpy_image[:, :, 3].tobytes()) wx_bitmap = wx_image.ConvertToBitmap() dc = wx.ClientDC(self.result_image_panel) dc.Clear() dc.DrawBitmap(wx_bitmap, (256 - numpy_image.shape[0]) // 2, (256 - numpy_image.shape[1]) // 2, True) toc = time.perf_counter() elapsed_time = toc - tic fps = min(1.0 / elapsed_time, 1000.0 / 33.0) self.fps_text.SetLabelText("FPS = %0.2f" % fps)
def _resize_canvas(self, w, h): self._array = np.zeros((h, w, 4), dtype=np.uint8) self._cv_image = converters.np_rgba_to_vtk(self._array) self.mapper.SetInputData(self._cv_image) self.mapper.Update() self.rgb = np.zeros((h, w, 3), dtype=np.uint8) self.alpha = np.zeros((h, w, 1), dtype=np.uint8) self.bitmap = wx.Bitmap.FromRGBA(w, h) try: self.image = wx.Image(w, h, self.rgb, self.alpha) except TypeError: self.image = wx.ImageFromBuffer(w, h, self.rgb, self.alpha) self.modified = True