def _cut_axes_and_save_svgs(figure, axes, x_lim, delta_total, data_name, tile_gradient_end, tile_gradient_st, preserve_ratio=False): for i, lh in enumerate(last_hours): file = os.path.join( path_dest, 'tile_{}_{}_last_{}h.svg'.format('enerpi_data', data_name, lh)) axes.set_xlim( (x_lim[0] + delta_total * (1 - lh / total_hours), x_lim[1])) figure.set_figwidth(_tile_figsize(lh / total_hours)[0]) write_fig_to_svg(figure, name_img=file, preserve_ratio=preserve_ratio) # if (i + 1 == len(last_hours)): if EXPORT_PNG_TILES and (i + 1 == len(last_hours)): path_png = file[:-3] + 'png' base_path, name_png = os.path.split(path_png) figure.set_dpi(216) canvas = FigureCanvas(figure) canvas.draw() # Fusion data + bg: data_img = Image.frombytes('RGBA', (900, 600), canvas.buffer_rgba()) png_img = _get_png_tile_background( os.path.join(base_path, 'fondo_' + name_png), (data_img.width, data_img.height), tile_gradient_end, tile_gradient_st) png_img.paste(data_img, (0, 0), data_img) png_img.save(path_png)
def generate_flat_surface_map(self, spot_radii, lon, lat): # we create an image using matplotlib (!!) fig = plt.figure(figsize=[5.00, 2.5], dpi=1200) proj = ccrs.PlateCarree() ax = plt.axes(projection=proj, fc="r") canvas = FigureCanvas(fig) plt.gca().set_position([0, 0, 1, 1]) ax.set_global() ax.outline_patch.set_linewidth(0.0) ax.set_extent([-180, 180, -90, 90]) # loop through each spot, adding it to the image # tissot assume the sphere is earth, so multiply by radius of earth for spot in range(self.spotnumber): add_spots = ax.tissot( rad_km=spot_radii[spot] * rEarth, lons=lon[spot], lats=lat[spot], n_samples=1000, fc="k", alpha=1, ) canvas.draw() buf = canvas.buffer_rgba() surface_map_image = np.asarray(buf) # 0 = photosphere # 1 = spot # 2 = planet surface_map = np.where(surface_map_image[:, :, 0] == 255, 0, 1) return surface_map
def encodes(self, o: TSTensor): device = o.device if o.data.device.type == 'cuda': o = o.cpu() if o.ndim == 2: o = o[None] nvars, seq_len = o.shape[-2:] aspect = seq_len / nvars size = ifnone(self.size, seq_len) fig = get_plot_fig(self.size, dpi=self.dpi) ax = fig.get_axes()[0] ax.set_xlim(0, seq_len - 1) canvas = FigureCanvasAgg(fig) output = [] for oi in o: if output == []: im = ax.imshow(oi, aspect=aspect, vmin=-1, vmax=1, cmap=self.cmap, **self.kwargs) else: im.set_data(oi) canvas.draw() buf = np.asarray(canvas.buffer_rgba())[..., :3] canvas.flush_events() output.append(tensor(buf / 255).permute(2, 0, 1)[None]) return TSImage(torch.cat(output)).to(device=device)
def _canvas_to_bytes(canvas: FigureCanvasAgg) -> ByteBuffer: # In matplotlib >= 3.1, canvas.buffer_rgba() returns a zero-copy memoryview. # This is faster to print to screen than the previous bytes. # Also the APIs are incompatible. # Flatten all dimensions of the memoryview. return canvas.buffer_rgba().cast("B")
def create_arrow_image(directions,pad=2): D = len(directions) assert D == 1,"Only one direction right now." W = 100 S = (W + pad) * D + pad arrows = np.zeros((S,W+2*pad,3)) direction = directions[0] for i in range(D): col_i = (pad+W)*i+pad canvas = arrows[col_i:col_i+W,pad:pad+W,:] start_point = (0,0) x_end = direction[0].item() y_end = direction[1].item() end_point = (x_end,y_end) fig = Figure(dpi=300) plt_canvas = FigureCanvas(fig) ax = fig.gca() ax.annotate("", xy=end_point, xycoords='data', xytext=start_point, textcoords='data', arrowprops=dict(arrowstyle="->",connectionstyle="arc3"), ) ax.set_xlim([-1,1]) ax.set_ylim([-1,1]) plt_canvas.draw() # draw the canvas, cache the renderer canvas = np.array(plt_canvas.buffer_rgba())[:,:,:] arrows = canvas arrows = torch.Tensor(arrows.astype(np.uint8)).transpose(0,2).transpose(1,2) return arrows
def vls_objmvment(self, img1, insmap, posepred): insmap_np = insmap[0].squeeze().cpu().numpy() posepred_np = posepred[0].cpu().numpy() xx, yy = np.meshgrid(range(insmap_np.shape[1]), range(insmap_np.shape[0]), indexing='xy') fig, ax = plt.subplots(figsize=(16,9)) canvas = FigureCanvasAgg(fig) ax.imshow(img1) for k in np.unique(insmap_np): if k == 0: continue xxf = xx[insmap_np == k] yyf = yy[insmap_np == k] xmin = xxf.min() xmax = xxf.max() ymin = yyf.min() ymax = yyf.max() if (ymax - ymin) * (xmax - xmin) < 1000: continue rect = patches.Rectangle((xmin, ymax), xmax - xmin, ymin - ymax, linewidth=1, facecolor='none', edgecolor='r') ax.add_patch(rect) ins_relpose = posepred_np[k] @ np.linalg.inv(posepred_np[0]) mvdist = np.sqrt(np.sum(ins_relpose[0:3, 3:4] ** 2)) ax.text(xmin + 5, ymin + 10, '%.3f' % mvdist, fontsize=6, c='r', weight='bold') plt.axis('off') canvas.draw() buf = canvas.buffer_rgba() plt.close() X = np.asarray(buf) return X
class VirtualCamWriter: def __init__(self, fps): self.fps = fps self.cam = None self.canvas = None self.fig = None def setup(self, fig, _, __): self.canvas = FigureCanvasAgg(fig) self.fig = fig def finish(self): self.cam.close() def grab_frame(self): self.canvas.draw() f = np.asarray(self.canvas.buffer_rgba()) LOG.debug('output frame shape: %s', f.shape) if self.cam is None: assert pyvirtualcam is not None self.cam = pyvirtualcam.Camera(f.shape[1], f.shape[0], self.fps) LOG.debug('virtual camera: %s', self.cam.device) else: self.cam.sleep_until_next_frame() self.cam.send(f[:, :, :3])
def imshow(self, data, *, keepdims=True): if isinstance(data, Figure): canvas = FigureCanvasAgg(data) canvas.draw() data = np.array(canvas.buffer_rgba()) self.data = data return self.render()
class Graph: def __init__(self, width, height, label_text): self.labels = label_text.split(' ') random.shuffle(self.labels) self.num_points = random.randint(min(3, len(self.labels)), max(7, len(self.labels))) dpi = 100 self.fig = Figure((math.ceil(width / dpi), math.ceil(height / dpi)), dpi=dpi) self.fig.patch.set_facecolor('none') self.fig.patch.set_alpha(0) self.canvas = FigureCanvas(self.fig) def generate_bar(self): ax = self.__get_sub_plot() ax.bar(self.__get_x_data(), self.__get_y_data(), color=self.__get_color()) def generate_line(self): ax = self.__get_sub_plot() ax.plot(self.__get_x_series(), self.__get_y_data(), color=self.__get_color()) def generate_pie(self): ax = self.__get_sub_plot() ax.pie(self.__get_y_data(), labels=self.__get_x_data()) ax.axis('equal') def __get_sub_plot(self): ax = self.fig.add_subplot(111) ax.set_facecolor('none') ax.set_alpha(0) return ax def get_image(self): self.canvas.draw() fig = self.canvas.figure size = (int(fig.get_figwidth() * fig.get_dpi()), int(fig.get_figheight() * fig.get_dpi())) return Image.frombytes("RGBA", size, self.canvas.buffer_rgba()) def __get_x_data(self): return self.labels[:self.num_points] def __get_x_series(self): return list(range(len(self.__get_y_data()))) def __get_y_data(self): return [random.randint(1, 10) for _ in range(self.num_points)] def __get_color(self): return random.choice(['red', 'green', 'blue'])
def get_img_from_fig(fig, dpi=180): fig.set_dpi(dpi) canvas = FigureCanvasAgg(fig) # Retrieve a view on the renderer buffer canvas.draw() buf = canvas.buffer_rgba() # convert to a NumPy array buf = np.asarray(buf) buf = Image.fromarray(buf) buf = buf.convert('RGB') return buf
def getImageArray(self, figure): # get the array from the matplotlib figure passed in canvas = FigureCanvasAgg(figure) canvas.draw() buf = canvas.buffer_rgba() # ... convert to a NumPy array ... X = np.asarray(buf, dtype=np.uint8) return X
def write_vls(self, image1, image2, flowgt, flow_predictions, valid, depth): img1 = image1[0].detach().cpu().detach().permute([1, 2, 0]).numpy().astype(np.uint8) img2 = image2[0].detach().cpu().detach().permute([1, 2, 0]).numpy().astype(np.uint8) validnp = valid[0].detach().cpu().numpy() == 1 depthnp = depth[0].squeeze().numpy() flow_pred = flow_to_image(flow_predictions[-1][0].permute([1, 2, 0]).detach().cpu().numpy(), rad_max=150) flow_gt = flow_to_image(flowgt[0].permute([1, 2, 0]).detach().cpu().numpy(), rad_max=150) h, w = image1.shape[2::] xx, yy = np.meshgrid(range(w), range(h), indexing='xy') pixelloc = np.stack([xx, yy], axis=0) pixelloc = torch.from_numpy(pixelloc).float() + flow_predictions[-1][0].detach().cpu() pixelloc[0, :, :] = ((pixelloc[0, :, :] / w) - 0.5) * 2 pixelloc[1, :, :] = ((pixelloc[1, :, :] / h) - 0.5) * 2 pixelloc = pixelloc.permute([1, 2, 0]) xxf = xx[validnp] yyf = yy[validnp] depthf = depthnp[validnp] xxf_oview = flowgt[0, 0].detach().cpu().numpy()[validnp] + xxf yyf_oview = flowgt[0, 1].detach().cpu().numpy()[validnp] + yyf vmax = 0.15 tnp = 1 / depthf / vmax tnp = self.cm(tnp) fig = plt.figure(figsize=(16, 2.5)) canvas = FigureCanvasAgg(fig) fig.add_subplot(1, 2, 1) plt.scatter(xxf, yyf, 1, tnp) plt.imshow(img1) fig.add_subplot(1, 2, 2) plt.scatter(xxf_oview, yyf_oview, 1, tnp) plt.imshow(img2) plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0) canvas.draw() buf = canvas.buffer_rgba() plt.close() X = np.asarray(buf) X = np.array(Image.fromarray(X).resize([w * 2, h], Image.BILINEAR)) image1_recon = torch.nn.functional.grid_sample(image2[0, :, :, :].detach().unsqueeze(0).cpu(), pixelloc.unsqueeze(0), mode='bilinear', align_corners=False) image1_recon = image1_recon[0].permute([1, 2, 0]).numpy().astype(np.uint8) img_mid = np.concatenate([flow_pred, flow_gt], axis=1) img_down = np.concatenate([img1, image1_recon], axis=1) img_vls = np.concatenate([X[:, :, 0:3], img_mid, img_down], axis=0) self.writer.add_image('img_vls', (torch.from_numpy(img_vls).float() / 255).permute([2, 0, 1]), self.total_steps)
def vls_sampling(self, img1, img2, depth, outputs): depth_np = depth[0].squeeze().cpu().numpy() h, w, _ = img1.shape xx, yy = np.meshgrid(range(w), range(h), indexing='xy') dsratio = 4 slRange_sel = (np.mod(xx, dsratio) == 0) * (np.mod(yy, dsratio) == 0) * (depth_np > 0) if np.sum(slRange_sel) > 0: xxfsl = xx[slRange_sel] yyfsl = yy[slRange_sel] rndidx = np.random.randint(0, xxfsl.shape[0], 1).item() xxfsl_sel = xxfsl[rndidx] yyfsl_sel = yyfsl[rndidx] slvlsxx_fg = (outputs['sample_pts'][0, :, int(yyfsl_sel / dsratio), int(xxfsl_sel / dsratio), 0].detach().cpu().numpy() + 1) / 2 * w slvlsyy_fg = (outputs['sample_pts'][0, :, int(yyfsl_sel / dsratio), int(xxfsl_sel / dsratio), 1].detach().cpu().numpy() + 1) / 2 * h else: slvlsxx_fg = None slvlsyy_fg = None cm = plt.get_cmap('magma') rndcolor = cm(1 / depth_np[yyfsl_sel, xxfsl_sel] / 0.15)[0:3] fig = plt.figure(figsize=(16, 9)) canvas = FigureCanvasAgg(fig) fig.add_subplot(2, 1, 1) plt.scatter(xxfsl_sel, yyfsl_sel, 10, [rndcolor]) plt.imshow(img1) plt.title("Input") fig.add_subplot(2, 1, 2) if slvlsxx_fg is not None and slvlsyy_fg is not None: plt.scatter(slvlsxx_fg, slvlsyy_fg, 5, np.random.rand(slvlsyy_fg.shape[0], 3)) plt.imshow(img2) plt.title("Sampling Arae") fig.tight_layout() # Or equivalently, "plt.tight_layout()" canvas.draw() buf = canvas.buffer_rgba() plt.close() X = np.asarray(buf) return X
def _update(frame): ff = None if update is not None: update(frame) ff = plt.gcf() else: ff = figs[frame] canvas = FigureCanvasAgg(ff) canvas.draw() rgba = np.asarray(canvas.buffer_rgba()) im = Image.fromarray(rgba) self.im.set_array(mpl.image.pil_to_array(im)) return self.im,
def requestImage(self, p_str, size): figure = self.getFigure(p_str) if figure is None: return QtQuick.QQuickImageProvider.requestImage(self, p_str, size) canvas = FigureCanvasAgg(figure) canvas.draw() w, h = canvas.get_width_height() img = QtGui.QImage(canvas.buffer_rgba(), w, h, QtGui.QImage.Format_RGBA8888).copy() return img, img.size()
def plot_to_image(arr, title="Histogram", vert_line_at=None): fig = Figure() canvas = FigureCanvasAgg(fig) ax = fig.gca() # ax.axis("off") y, x, _ = ax.hist(arr, bins=16) fig.suptitle(title) if vert_line_at: print(f"Plotting vertical line at: {vert_line_at} {y.max()}") ax.axvline(x=vert_line_at, ymin=0, ymax=y.max(), color="r") canvas.draw() plot_image = np.asarray(canvas.buffer_rgba()) return plot_image
def vls_sampling(self, img1, img2, depthgt, flowpred, outputs): depthgtnp = depthgt[0].squeeze().cpu().numpy() h, w, _ = img1.shape xx, yy = np.meshgrid(range(w), range(h), indexing='xy') selector = (depthgtnp > 0) slRange_sel = (np.mod(xx, 4) == 0) * (np.mod(yy, 4) == 0) * selector dsratio = 4 xxfsl = xx[slRange_sel] yyfsl = yy[slRange_sel] rndidx = np.random.randint(0, xxfsl.shape[0], 1).item() xxfsl_sel = xxfsl[rndidx] yyfsl_sel = yyfsl[rndidx] slvlsxx_fg = (outputs['sample_pts'][ 0, :, int(yyfsl_sel / dsratio), int(xxfsl_sel / dsratio), 0].detach().cpu().numpy() + 1) / 2 * w slvlsyy_fg = (outputs['sample_pts'][ 0, :, int(yyfsl_sel / dsratio), int(xxfsl_sel / dsratio), 1].detach().cpu().numpy() + 1) / 2 * h flow_predx = flowpred[0, 0, yyfsl_sel, xxfsl_sel].cpu().numpy() flow_predy = flowpred[0, 1, yyfsl_sel, xxfsl_sel].cpu().numpy() fig = plt.figure(figsize=(16, 9)) canvas = FigureCanvasAgg(fig) fig.add_subplot(2, 1, 1) plt.imshow(img1) plt.scatter(xxfsl_sel, yyfsl_sel, 3, 'r') plt.title("Input") fig.add_subplot(2, 1, 2) plt.scatter(slvlsxx_fg, slvlsyy_fg, 3, 'b') plt.scatter(xxfsl_sel + flow_predx, yyfsl_sel + flow_predy, 3, 'r') plt.imshow(img2) plt.title("Sampling Arae") fig.tight_layout() # Or equivalently, "plt.tight_layout()" canvas.draw() buf = canvas.buffer_rgba() plt.close() X = np.asarray(buf) return X
def distributions_plot(s2_values: np.ndarray, deimos_values: np.ndarray, sr_values: np.ndarray, band: int) -> np.ndarray: """ Return plot image histogram of S2, Deimos and SR distributions for a particular band. s2_values: np.ndarray: NxCxWxH array of S2 values deimos_values: np.ndarray: NxCxWxH array of deimos values sr_values: np.ndarray: NxCxWxH array of predicted super resolved images return: np.ndarray: matplotlib plot of the distributions converted to numpy array image (so it can be passed to WANDB). """ # make a Figure and attach it to a canvas. fig = Figure(figsize=(15, 7), dpi=300) canvas = FigureCanvasAgg(fig) # Do some plotting here ax = fig.add_subplot(1, 1, 1) ax.hist(s2_values[:, band, ...].flatten(), alpha=.33, bins=100, range=(-2, 2), label='S2', density=True) ax.hist(deimos_values[:, band, ...].flatten(), alpha=.33, bins=100, range=(-2, 2), label='Deimos', density=True) ax.hist(sr_values[:, band, ...].flatten(), alpha=.33, bins=100, label='SR', range=(-2, 2), density=True, histtype='step') ax.set_title(f'Band {band}') ax.legend() # Retrieve a view on the renderer buffer canvas.draw() buf = canvas.buffer_rgba() # convert to a NumPy array X = np.asarray(buf) return X
def plot_source_rgb_raw(source_series, times, t, size, dpi): fig = plt.Figure(figsize=size, dpi=dpi) canvas = FigureCanvasAgg(fig) # Do some plotting here ax = fig.add_subplot(111) ax.plot(times, source_series, "-*") ax.axvline(t, color="red") ax.set_xlabel("t in s") ax.set_ylabel("g in 1/s") ax.grid() fig.tight_layout() # Retrieve a view on the renderer buffer canvas.draw() buf = canvas.buffer_rgba() # convert to a NumPy array data = np.asarray(buf) return data[:, :, :]
def encodes(self, o: TSTensor): device = o.device if o.data.device.type == 'cuda': o = o.cpu() if o.ndim == 2: o = o[None] seq_len = o.shape[-1] fig = self.fig if self.size is None: fig.set_size_inches(seq_len / self.dpi, seq_len / self.dpi) canvas = FigureCanvasAgg(fig) ax = fig.get_axes()[0] ax.set_xlim(0, seq_len - 1) output = [] for oi in o: start = time.time() ax.plot(oi.T, lw=self.lw, **self.kwargs) canvas.draw() buf = np.asarray(canvas.buffer_rgba())[..., :3] output.append(tensor(buf / 255).permute(2,0,1)[None]) del ax.lines[:len(ax.lines)] return TSImage(torch.cat(output)).to(device=device)
def __init__(self, figs=None, update=None, frames=None, *args, **kwargs): # Use the list of figures as the framedata, which will be iterated # over by the machinery. if update is None: f1 = figs[0] else: update(frames[0]) f1 = plt.gcf() plt.clf() plt.close() self._figs = figs f = plt.figure() plt.axis('off') canvas = FigureCanvasAgg(f1) canvas.draw() rgba = np.asarray(canvas.buffer_rgba()) im = Image.fromarray(rgba) self.im = plt.imshow(im) def _update(frame): ff = None if update is not None: update(frame) ff = plt.gcf() else: ff = figs[frame] canvas = FigureCanvasAgg(ff) canvas.draw() rgba = np.asarray(canvas.buffer_rgba()) im = Image.fromarray(rgba) self.im.set_array(mpl.image.pil_to_array(im)) return self.im, animation.FuncAnimation.__init__( self, f, _update, frames=len(figs) if update is None else frames, *args, **kwargs)
def plotDotOnImage(image, dot): fig, ax = windowRenderer() canvas = FigureCanvas(fig) ax.imshow(image) # PLOTTING : plot the dot to the x, y position ax.plot(dot['x'], dot['y'], 'bo') # ro = bleu , bo = rouge, go = green canvas.draw() buf = canvas.buffer_rgba() # convert to a NumPy array X = np.asarray(buf) # X = l'image non utilisée # Remove this for test the matplotlib plot # plt.show() cv2.imshow('frame', X)
def generate_thumbnail_content(gpx_track): fig = Figure(figsize=(1, 1), dpi=128, facecolor="white", linewidth=2, tight_layout=True) canvas = FigureCanvasAgg(fig) ax = fig.add_subplot(aspect="equal") ax.set_axis_off() for segment in gpx_track.segments: lat = [] long = [] for point in segment.points: lat.append(point.latitude) long.append(point.longitude) ax.plot(long, lat, color="blue") canvas.draw() return np.asarray(canvas.buffer_rgba())
def _cut_axes_and_save_svgs(figure, axes, x_lim, delta_total, data_name, tile_gradient_end, tile_gradient_st, preserve_ratio=False): for i, lh in enumerate(last_hours): file = os.path.join(path_dest, 'tile_{}_{}_last_{}h.svg'.format('enerpi_data', data_name, lh)) axes.set_xlim((x_lim[0] + delta_total * (1 - lh / total_hours), x_lim[1])) figure.set_figwidth(_tile_figsize(lh / total_hours)[0]) write_fig_to_svg(figure, name_img=file, preserve_ratio=preserve_ratio) # if (i + 1 == len(last_hours)): if EXPORT_PNG_TILES and (i + 1 == len(last_hours)): path_png = file[:-3] + 'png' base_path, name_png = os.path.split(path_png) figure.set_dpi(216) canvas = FigureCanvas(figure) canvas.draw() # Fusion data + bg: data_img = Image.frombytes('RGBA', (900, 600), canvas.buffer_rgba()) png_img = _get_png_tile_background(os.path.join(base_path, 'fondo_' + name_png), (data_img.width, data_img.height), tile_gradient_end, tile_gradient_st) png_img.paste(data_img, (0, 0), data_img) png_img.save(path_png)
def plotTrajectory3D(framerate): sleep_time = round(1000. / framerate) fig = plt.figure() canvas = FigureCanvas(fig) ax = fig.add_subplot(111, projection='3d') x_arr, y_arr, z_arr = [], [], [] for image_index in range(num_images): corners_2D = detected_corners[image_index, :].reshape((-1, 2)) corners_3D = p_W_corners M = estimatePoseDLT(corners_2D, corners_3D, K) T = M[:, 3] R = M[:3, :3] R_inv = np.transpose(R) T_new = -R_inv @ T x_arr.append(T_new[0]) y_arr.append(T_new[1]) z_arr.append(T_new[2]) u, v, w = R_inv[:, 0], R_inv[:, 1], R_inv[:, 2] plt.cla() ax.plot(x_arr, y_arr, z_arr, 'b') ax.quiver(x_arr[-1], y_arr[-1], z_arr[-1], u, v, w, length=0.3, normalize=True) canvas.draw() buf = canvas.buffer_rgba() image = np.asarray(buf) cv.imshow("Trajectory", image) cv.waitKey(sleep_time)
def heatmapWhiteFrame(image, dot): """ permet de faire le plotting de la heatmap sur l'image passée en paramètre :param : image : chaque frame de la vidéo :param : dot : le point permettant de tracer la heatmap :return retourne l'image modifiée sous la forme d'un tableau de tableaux """ fig, ax = windowRenderer() canvas = FigureCanvas(fig) ax.imshow(image) # PLOTTING : plot the dot to the x, y position # ro : bleu, markersize: taille du cercle, alpha: opacité ax.plot(dot['x'], dot['y'], 'bo', markersize=30, alpha=0.01) canvas.draw() buf = canvas.buffer_rgba() # convert to a NumPy array X = np.asarray(buf) # X = image retournée return X
# A canvas must be manually attached to the figure (pyplot would automatically # do it). This is done by instantiating the canvas with the figure as # argument. canvas = FigureCanvasAgg(fig) # Do some plotting. ax = fig.add_subplot() ax.plot([1, 2, 3]) # Option 1: Save the figure to a file; can also be a file-like object (BytesIO, # etc.). fig.savefig("test.png") # Option 2: Retrieve a view on the renderer buffer... canvas.draw() buf = canvas.buffer_rgba() # ... convert to a NumPy array ... X = np.asarray(buf) # ... and pass it to PIL. from PIL import Image im = Image.fromarray(X) # Uncomment this line to display the image using ImageMagick's `display` tool. # im.show() ############################################################################# # # .. admonition:: References # # The use of the following functions, methods, classes and modules is shown # in this example:
def vls_sampling(self, img1, img2, depthgt, flowmap, insmap, outputs): depthgtnp = depthgt[0].squeeze().cpu().numpy() insmapnp = insmap[0].squeeze().cpu().numpy() flowmapnp = flowmap[0].cpu().numpy() h, w, _ = img1.shape xx, yy = np.meshgrid(range(w), range(h), indexing='xy') selector = (depthgtnp > 0) flowx = outputs[('flowpred', 2)][0, 0].detach().cpu().numpy() flowy = outputs[('flowpred', 2)][0, 1].detach().cpu().numpy() flowxf = flowx[selector] flowyf = flowy[selector] floworgx = outputs['org_flow'][0, 0].detach().cpu().numpy() floworgy = outputs['org_flow'][0, 1].detach().cpu().numpy() floworgxf = floworgx[selector] floworgyf = floworgy[selector] xxf = xx[selector] yyf = yy[selector] df = depthgtnp[selector] slRange_sel = (np.mod(xx, 4) == 0) * (np.mod(yy, 4) == 0) * selector * (insmapnp > 0) dsratio = 4 if np.sum(slRange_sel) > 0: xxfsl = xx[slRange_sel] yyfsl = yy[slRange_sel] rndidx = np.random.randint(0, xxfsl.shape[0], 1).item() xxfsl_sel = xxfsl[rndidx] yyfsl_sel = yyfsl[rndidx] slvlsxx_fg = (outputs['sample_pts'][0, :, int(yyfsl_sel / dsratio), int(xxfsl_sel / dsratio), 0].detach().cpu().numpy() + 1) / 2 * w slvlsyy_fg = (outputs['sample_pts'][0, :, int(yyfsl_sel / dsratio), int(xxfsl_sel / dsratio), 1].detach().cpu().numpy() + 1) / 2 * h else: slvlsxx_fg = None slvlsyy_fg = None slRange_sel = (np.mod(xx, 4) == 0) * (np.mod(yy, 4) == 0) * selector * (insmapnp == 0) if np.sum(slRange_sel) > 0: xxfsl = xx[slRange_sel] yyfsl = yy[slRange_sel] rndidx = np.random.randint(0, xxfsl.shape[0], 1).item() xxfsl_sel = xxfsl[rndidx] yyfsl_sel = yyfsl[rndidx] slvlsxx_bg = (outputs['sample_pts'][0, :, int(yyfsl_sel / dsratio), int(xxfsl_sel / dsratio), 0].detach().cpu().numpy() + 1) / 2 * w slvlsyy_bg = (outputs['sample_pts'][0, :, int(yyfsl_sel / dsratio), int(xxfsl_sel / dsratio), 1].detach().cpu().numpy() + 1) / 2 * h gtposx = xxfsl_sel + flowmapnp[0, yyfsl_sel, xxfsl_sel] gtposy = yyfsl_sel + flowmapnp[0, yyfsl_sel, xxfsl_sel] else: slvlsxx_bg = None slvlsyy_bg = None cm = plt.get_cmap('magma') rndcolor = cm(1 / df / 0.15)[:, 0:3] fig = plt.figure(figsize=(16, 9)) canvas = FigureCanvasAgg(fig) fig.add_subplot(2, 2, 1) plt.scatter(xxf, yyf, 3, rndcolor) plt.imshow(img1) plt.title("Input") fig.add_subplot(2, 2, 2) plt.scatter(xxf + floworgxf, yyf + floworgyf, 3, rndcolor) plt.imshow(img2) plt.title("Original Prediction") fig.add_subplot(2, 2, 3) plt.scatter(xxf + flowxf, yyf + flowyf, 3, rndcolor) plt.imshow(img2) plt.title("Fixed Prediction") fig.add_subplot(2, 2, 4) if slvlsxx_fg is not None and slvlsyy_fg is not None: plt.scatter(slvlsxx_fg, slvlsyy_fg, 3, 'b') plt.scatter(slvlsxx_fg[16], slvlsyy_fg[16], 3, 'g') if slvlsxx_fg is not None and slvlsyy_fg is not None: plt.scatter(slvlsxx_bg, slvlsyy_bg, 3, 'b') plt.scatter(slvlsxx_bg[16], slvlsyy_bg[16], 3, 'g') plt.scatter(gtposx, gtposy, 3, 'r') plt.imshow(img2) plt.title("Sampling Arae") fig.tight_layout() # Or equivalently, "plt.tight_layout()" canvas.draw() buf = canvas.buffer_rgba() plt.close() X = np.asarray(buf) # Image.fromarray(X).show() return X
# do it). This is done by instantiating the canvas with the figure as # argument. canvas = FigureCanvasAgg(fig) # Do some plotting. ax = fig.add_subplot() ax.plot([1, 2, 3]) # Option 1: Save the figure to a file; can also be a file-like object (BytesIO, # etc.). fig.savefig("test.png") # Option 2: Retrieve a memoryview on the renderer buffer, and convert it to a # numpy array. canvas.draw() rgba = np.asarray(canvas.buffer_rgba()) # ... and pass it to PIL. im = Image.fromarray(rgba) # This image can then be saved to any format supported by Pillow, e.g.: im.save("test.bmp") # Uncomment this line to display the image using ImageMagick's `display` tool. # im.show() ############################################################################# # # .. admonition:: References # # The use of the following functions, methods, classes and modules is shown # in this example: #
def fig2buf(fig): canvas = FigureCanvasAgg(fig) fig.canvas.draw() return np.asarray(canvas.buffer_rgba())[..., :3]
cubeTexture.InterpolateOn() cubeTexture.SetInput(imflip.GetOutput()) cubeActor.SetTexture(cubeTexture) ren = vtkRenderer() ren.AddActor(cubeActor) win = plugInManager['VtkWindow'].winManager.newWindow() win.vtkWidget.GetRenderWindow().AddRenderer(ren) # Now create our plot fig = Figure() canvas = FigureCanvasAgg(fig) ax = fig.add_subplot(111) ax.grid(True) ax.set_xlabel('Hello from VTK!', size=16) ax.bar(xrange(10), p.rand(10)) # Powers of 2 image to be clean w,h = 1024, 1024 dpi = canvas.figure.get_dpi() fig.set_figsize_inches(w / dpi, h / dpi) canvas.draw() # force a draw # This is where we tell the image importer about the mpl image extent = (0, w - 1, 0, h - 1, 0, 0) importer.SetWholeExtent(extent) importer.SetDataExtent(extent) importer.SetImportVoidPointer(canvas.buffer_rgba(0,0), 1) importer.Update()
def invoke(self, context, event): if mp == 1: simnode = bpy.data.node_groups[self.nodeid.split('@')[1]].nodes[self.nodeid.split('@')[0]] locnode = simnode.inputs[0].links[0].from_node scene, scene.resnode, scene.restree = context.scene, simnode.name, self.nodeid.split('@')[1] scene.vi_display, scene.sp_disp_panel, scene.li_disp_panel, scene.lic_disp_panel, scene.en_disp_panel, scene.ss_disp_panel, scene.wr_disp_panel = 1, 0, 0, 0, 0, 0, 1 with open(locnode.weather, "r") as epwfile: if locnode.startmonth > locnode.endmonth: self.report({'ERROR'},"Start month is later than end month") return {'FINISHED'} else: wvals = [line.split(",")[20:22] for l, line in enumerate(epwfile.readlines()) if l > 7 and locnode.startmonth <= int(line.split(",")[1]) < locnode.endmonth] simnode['maxres'], simnode['minres'], simnode['avres']= max([float(w[1]) for w in wvals]), min([float(w[1]) for w in wvals]), sum([float(w[1]) for w in wvals])/len(wvals) awd, aws, (fig, ax) = [float(val[0]) for val in wvals], [float(val[1]) for val in wvals], wr_axes() binvals = arange(0,int(ceil(max(aws))),2) simnode['nbins'] = len(binvals) if simnode.wrtype == '0': ax.bar(awd, aws, bins=binvals, normed=True, opening=0.8, edgecolor='white') if simnode.wrtype == '1': ax.box(awd, aws, bins=binvals, normed=True) if simnode.wrtype == '2': ax.contourf(awd, aws, bins=binvals, normed=True, cmap=cm.hot) if simnode.wrtype == '3': ax.contourf(awd, aws, bins=binvals, normed=True, cmap=cm.hot) ax.contour(awd, aws, bins=binvals, normed=True, colors='black') if simnode.wrtype == '4': ax.contour(awd, aws, bins=binvals, normed=True, cmap=cm.hot) if locnode.newdir: if str(sys.platform) != 'win32': plt.savefig(locnode.newdir+'/disp_wind.png', dpi = (150), transparent=False) if 'disp_wind.png' not in [im.name for im in bpy.data.images]: bpy.data.images.load(locnode.newdir+'/disp_wind.png') else: bpy.data.images['disp_wind.png'].filepath = locnode.newdir+'/disp_wind.png' bpy.data.images['disp_wind.png'].reload() # Below is a workaround for the matplotlib/blender png bug else: canvas = FigureCanvasAgg(fig) canvas.draw() pixbuffer, pixels = canvas.buffer_rgba(), [] [w, h] = [int(d) for d in fig.bbox.bounds[2:]] pixarray = numpy.frombuffer(pixbuffer, numpy.uint8) pixarray.shape = h, w, 4 for hi in reversed(pixarray): for wi in hi: pixels += [p/255 for p in wi] if 'disp_wind.png' not in [im.name for im in bpy.data.images]: wrim = bpy.data.images.new('disp_wind.png', height = h, width = w) wrim.file_format = 'PNG' wrim.filepath = locnode.newdir+os.sep+wrim.name wrim.save() else: wrim = bpy.data.images['disp_wind.png'] wrim.pixels = pixels wrim.update() wrim.save() wrim.reload() plt.savefig(locnode.newdir+'/disp_wind.svg') else: self.report({'ERROR'},"No project directory. Save the Blender file and recreate the VI Location node.") return {'CANCELLED'} if 'Wind_Plane' not in [ob.get('VIType') for ob in bpy.context.scene.objects]: bpy.ops.mesh.primitive_plane_add(enter_editmode=False, location=(0.0, 0.0, 0.0)) bpy.context.active_object['VIType'] = 'Wind_Plane' wind_mat = bpy.data.materials.new('Wind_Rose') tex = bpy.data.textures.new(type = 'IMAGE', name = 'Wind_Tex') tex.image = bpy.data.images['disp_wind.png'] wind_mat.texture_slots.add() wind_mat.texture_slots[0].texture = tex wind_mat.texture_slots[0].use_map_alpha = True bpy.context.active_object.name = "Wind_Plane" bpy.ops.object.material_slot_add() bpy.context.active_object.material_slots[0].material = wind_mat bpy.context.active_object.data.uv_textures.new() bpy.context.active_object.data.uv_textures[0].data[0].image = bpy.data.images['disp_wind.png'] bpy.context.active_object.scale = (100, 100, 100) wind_mat.use_transparency = False wind_mat.transparency_method = 'Z_TRANSPARENCY' wind_mat.alpha = 0.0 bpy.ops.view3d.wrlegdisplay('INVOKE_DEFAULT') return {'FINISHED'} else: self.report({'ERROR'},"There is something wrong with your matplotlib installation") return {'FINISHED'}