def new_frame(pipe_ep): global canvas, ctx #Loop to get the newest frame while (pipe_ep.Available > 0): #Receive the packet image = pipe_ep.ReceivePacket() #Convert the packet to an image and set the global variable if (canvas == None): canvas = document.getElementById("image") ctx = canvas.getContext("2d") imageBytes = np.zeros( 4 * image.width * image.height, dtype=np.uint8) #dtype essential here, IndexSizeError for y in range(image.height): for x in range(image.width): index1 = (x + image.width * y) * 4 index2 = (x * 3 + image.step * y) imageBytes[index1] = image.data[index2 + 2] imageBytes[index1 + 1] = image.data[index2 + 1] imageBytes[index1 + 2] = image.data[index2] imageBytes[index1 + 3] = 255 image_data = ImageData.new(bytes(imageBytes), image.width, image.height) ctx.putImageData(image_data, 0, 0, 0, 0, 320, 240)
def draw(self): from pyodide import create_proxy # Render the figure using Agg self._idle_scheduled = True orig_dpi = self.figure.dpi if self._ratio != 1: self.figure.dpi *= self._ratio pixels_proxy = None pixels_buf = None try: super().draw() # Copy the image buffer to the canvas width, height = self.get_width_height() canvas = self.get_element("canvas") if canvas is None: return pixels = self.buffer_rgba().tobytes() pixels_proxy = create_proxy(pixels) pixels_buf = pixels_proxy.getBuffer("u8clamped") image_data = ImageData.new(pixels_buf.data, width, height) ctx = canvas.getContext("2d") ctx.putImageData(image_data, 0, 0) finally: self.figure.dpi = orig_dpi self._idle_scheduled = False if pixels_proxy: pixels_proxy.destroy() if pixels_buf: pixels_buf.release()
def draw_image(self, gc, x, y, im, transform=None): im = np.flipud(im) h, w, d = im.shape y = self.ctx.height - y - h im = np.ravel(np.uint8(np.reshape(im, (h * w * d, -1)))).tobytes() pixels_proxy = create_proxy(im) pixels_buf = pixels_proxy.getBuffer("u8clamped") img_data = ImageData.new(pixels_buf.data, w, h) self.ctx.save() in_memory_canvas = document.createElement("canvas") in_memory_canvas.width = w in_memory_canvas.height = h in_memory_canvas_context = in_memory_canvas.getContext("2d") in_memory_canvas_context.putImageData(img_data, 0, 0) self.ctx.drawImage(in_memory_canvas, x, y, w, h) self.ctx.restore() pixels_proxy.destroy() pixels_buf.release()
def draw(self): # Render the figure using Agg self._idle_scheduled = True orig_dpi = self.figure.dpi if self._ratio != 1: self.figure.dpi *= self._ratio try: super().draw() # Copy the image buffer to the canvas width, height = self.get_width_height() canvas = self.get_element("canvas") if canvas is None: return image_data = ImageData.new(self.buffer_rgba(), width, height) ctx = canvas.getContext("2d") ctx.putImageData(image_data, 0, 0) finally: self.figure.dpi = orig_dpi self._idle_scheduled = False
def new_frame(pipe_ep): global canvas, ctx #Loop to get the newest frame while (pipe_ep.Available > 0): #Receive the packet image=pipe_ep.ReceivePacket() #Convert the packet to an image and set the global variable if (canvas == None): canvas = document.getElementById("image") ctx = canvas.getContext("2d") imageBytes=np.zeros(4*image.width*image.height, dtype=np.uint8) #dtype essential here, IndexSizeError imageBytes[3::4] = 255 imageBytes[0::4] = image.data[2::3] imageBytes[1::4] = image.data[1::3] imageBytes[2::4] = image.data[0::3] image_data=ImageData.new(bytes(imageBytes),image.width,image.height) ctx.putImageData(image_data, 0, 0,0,0,320,240)