예제 #1
0
 def getFrame(self, framenum):
     self.data.seek(max(self.FrameSize * (framenum - 1), 0))
     byt = self.data.read(self.FrameSize)
     data = np.ndarray(shape=(self.NumVectors, self.PixPerVector),
                       buffer=byt,
                       dtype='uint8').swapaxes(0, 1)
     fig = plt.figure()
     fig.patch.set_facecolor('black')
     ax = fig.add_subplot(111, polar='True')
     ax.axis('off')
     ax.set_thetamin(0)
     ax.set_thetamax(180)
     ax.pcolormesh(self.thetaspace, self.radspace, data, cmap='gray')
     canvas = FigureCanvasAgg(fig)
     canvas.draw()
     r = self.ZeroOffset + self.PixPerVector
     px = ax.transData.transform([[0, 0], [math.pi / 2, r],
                                  [self.thetaspace[0], r],
                                  [self.thetaspace[-1], r]])
     xmin = math.floor(px[2][0])
     xmax = math.ceil(px[3][0])
     ymin = math.floor(px[0][1])
     ymax = math.ceil(px[1][1])
     ret = Image.frombytes('RGB', canvas.get_width_height(),
                           canvas.tostring_rgb())
     plt.close(fig)
     return ret.crop((xmin, ymin, xmax, ymax))
예제 #2
0
def render_history():
    figure, axes = plt.subplots(2)
    canvas = FigureCanvas(figure)

    axes[0].plot(reward_history, 'red')
    axes[0].plot(smooth(reward_history, 4), 'orange')
    axes[0].plot(smooth(reward_history, 8), 'yellow')
    axes[0].plot(smooth(reward_history, 16), 'green')
    axes[0].plot(smooth(reward_history, 32), 'blue')
    axes[0].plot(smooth(reward_history, 64), 'purple')
    axes[1].plot(loss_history, 'red')
    axes[1].plot(smooth(loss_history, 4), 'orange')
    axes[1].plot(smooth(loss_history, 8), 'yellow')
    axes[1].plot(smooth(loss_history, 16), 'green')
    axes[1].plot(smooth(loss_history, 32), 'blue')
    axes[1].plot(smooth(loss_history, 64), 'purple')
    canvas.draw()
    image = np.fromstring(canvas.tostring_rgb(), dtype='uint8')

    img = image.reshape(canvas.get_width_height()[::-1] + (3,))

    # img is rgb, convert to opencv's default bgr
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

    # display image with opencv or any operation you like
    cv2.imshow("plot", img)
    plt.close('all')
예제 #3
0
def make_preview(track, world=None, title=None):
    fig = Figure(figsize=(10, 10))
    if title: fig.suptitle(title)

    canvas = FigureCanvas(fig)
    ax = fig.gca()

    ts = np.linspace(0.0, track.e_t, num=10000)
    x = [track.x_t(t) for t in ts]
    y = [track.y_t(t) for t in ts]
    ax.plot(x, y)

    if not world: world = World()
    track.add_to(world)
    x = [cone['x'] for cone in world.cones]
    y = [cone['y'] for cone in world.cones]
    ax.scatter(x, y)

    top, bottom = min(y), max(y)
    left, right = min(x), max(x)
    size = max(bottom - top + 10, right - left + 10) / 2.0

    center_y = (bottom + top) / 2.0
    center_x = (right + left) / 2.0
    ax.set_xlim([center_x-size, center_x+size])
    ax.set_ylim([center_y-size, center_y+size])

    canvas.draw()
    image = np.fromstring(canvas.tostring_rgb(), dtype='uint8')
    image = image.reshape(canvas.get_width_height()[::-1] + (3,))
    return image
예제 #4
0
def histogram(year, months, earns, pays):
    '''
    根据年度汇总绘制每个月收支的直方图
    :param year:年度
    :param months:月份数
    :param earns:每月收入汇总
    :param pays:每月支出汇总
    :return:image->np.array
    '''
    plt.bar(x=months, height=earns, label='收入', color='steelblue', alpha=0.8)
    plt.bar(x=months, height=pays, label='支出', color='indianred', alpha=0.8)
    # 在柱状图上显示具体数值, ha参数控制水平对齐方式, va控制垂直对齐方式
    for x, y in enumerate(earns):
        plt.text(x, y, '%s元' % y, ha='center', va='bottom')
    for x, y in enumerate(pays):
        plt.text(x, y, '%s元' % y, ha='center', va='top')
    # # 设置标题
    plt.title("{}年各月收入支出柱状图".format(year))
    # 为两条坐标轴设置名称
    plt.xlabel("月份")
    plt.ylabel("收支金额")
    # 显示图例
    plt.legend()
    canvas = FigureCanvasAgg(plt.gcf())
    canvas.draw()
    w, h = canvas.get_width_height()
    img = np.frombuffer(canvas.tostring_argb(), dtype=np.uint8)
    img.shape = (w, h, 4)
    img = np.roll(img, 3, axis=2)
    img = Image.frombytes("RGBA", (w, h), img.tostring())
    plt.close()
    return img
예제 #5
0
def figure_to_image(fig):
    canvas = FigureCanvas(fig)
    canvas.draw()
    data = np.frombuffer(canvas.buffer_rgba(), dtype=np.uint8)
    w, h = canvas.get_width_height()
    image_hwc = data.reshape([h, w, 4])[:, :, 0:3]
    return np.moveaxis(image_hwc, source=2, destination=0)
예제 #6
0
def compute_metrics(result_file, gt_file):
    # groundtruth
    with open(gt_file, 'r', newline='') as csv_file:
        reader = csv.reader(csv_file, delimiter=',')
        gt = [int(row[1]) for row in reader]
    # prediction
    pred = []
    i = 0
    with open(result_file, 'r', newline='') as csv_file:
        reader = csv.reader(csv_file, delimiter=',')
        for row in reader:
            prob = list(map(float, row))
            pred.append(prob[1])
            i += 1
    # compute mAP
    mAP = average_precision_score(gt, pred, average='macro')
    # compute AUC
    AUC = roc_auc_score(gt, pred)
    # plot ROC curve
    precision, recall, __ = precision_recall_curve(gt, pred, pos_label=1)
    fig = Figure()
    canvas = FigureCanvasAgg(fig)
    ax = fig.gca()
    ax.step(recall, precision, color='b', alpha=0.2, where='post')
    ax.fill_between(recall, precision, step='post', alpha=0.2, color='b')
    ax.set_xlabel('Recall')
    ax.set_ylabel('Precision')
    ax.set_ylim([0.0, 1.05])
    ax.set_xlim([0.0, 1.0])
    canvas.draw()
    I = np.fromstring(canvas.tostring_rgb(), dtype='uint8', sep='')
    I = I.reshape(canvas.get_width_height()[::-1]+(3,))
    I = np.transpose(I, [2,0,1])
    return mAP, AUC, torch.Tensor(np.float32(I))
예제 #7
0
def aps_img(dict_aps):
    xticks = []
    values = []
    for key, value in dict_aps.items():
        values.append(value)
        xticks.append(key)
        
    values = np.array(values)
    xticks = np.array(xticks)

    index = values.argsort()
    values = values[index][::-1]
    xticks = xticks[index][::-1]

    plt.rcdefaults()
    fig, ax = plt.subplots(figsize=(20,80))
    canvas = FigureCanvas(fig)
    ax.barh(range(len(values)), values)
    ax.set_yticks(range(len(values)))
    ax.set_yticklabels(xticks)
    ax.invert_yaxis() 
    ax.set_xlabel('AP')
    ax.set_title('mAp')
    ax.set_xlim([0, 1])
    plt.tight_layout()
    canvas.draw()
    width, height = fig.get_size_inches() * fig.get_dpi()
    pil_image = PIL.Image.frombytes('RGB', canvas.get_width_height(), canvas.tostring_rgb())
    plt.close()
    return pil_image
예제 #8
0
def __create_graph(data, allIndexes, okIndexes, title, ylabel):
    fig = Figure(figsize=(4, 4), dpi=256)
    ax = fig.add_subplot(111)

    ax.plot(data[0], label="DAPI", color='red')
    ax.plot(data[1], label="Alx", color='blue')

    ax.axvline(min(allIndexes[0][0], allIndexes[1][0]), color='purple')
    ax.axvline(max(allIndexes[0][1], allIndexes[1][1]), color='purple')

    ax.axvline(okIndexes[0], color='green')
    ax.axvline(okIndexes[1], color='green')

    ax.set_title(title, fontsize=20)
    ax.set_xlabel('Slices', fontsize=18)
    ax.set_ylabel(ylabel, fontsize=18)
    ax.legend()

    canvas = FigureCanvasAgg(fig)
    canvas.draw()

    figRGB = canvas.renderer.tostring_rgb()
    ncols, nrows = canvas.get_width_height()
    figNP = np.fromstring(figRGB, dtype=np.uint8).reshape(nrows, ncols, 3)

    return figNP
예제 #9
0
def get_bar_plot_np(counts, t=10, label=''):
    x = np.arange(len(counts))
    x1 = x[::-1] * (-t)
    xlab = np.array(["{}s".format(i) for i in x1])
    xlab[-1] = 'NOW'

    if len(counts) > 7:
        xlab[::2] = ''

    fig = Figure(figsize=(3.0, 1.8))

    canvas = FigureCanvas(fig)
    ax = fig.gca()
    ax.bar(x, counts)
    ax.set_facecolor('#c8c8c8')
    ax.set_xticks(x)
    ax.set_xticklabels(xlab)  #, rotation=30)
    #ax.axis('off')

    w, h = canvas.get_width_height()
    ax.set_title(label)
    canvas.draw()  # draw the canvas, cache the renderer

    np_img = np.fromstring(canvas.tostring_rgb(), dtype='uint8')
    np_img = np_img.reshape((h, w, 3))

    return np_img
예제 #10
0
def get_image_plot_crossfield(crossfield, crossfield_stride):
    fig = Figure(figsize=(crossfield.shape[1] / 100,
                          crossfield.shape[0] / 100),
                 dpi=100)
    canvas = FigureCanvasAgg(fig)
    ax = fig.gca()

    plot_crossfield(ax,
                    crossfield,
                    crossfield_stride,
                    alpha=1.0,
                    width=2.0,
                    add_scale=1)

    ax.axis('off')
    fig.tight_layout(pad=0)
    # To remove the huge white borders
    ax.margins(0)

    canvas.draw()
    image_from_plot = np.frombuffer(canvas.tostring_argb(), dtype=np.uint8)
    image_from_plot = image_from_plot.reshape(canvas.get_width_height()[::-1] +
                                              (4, ))
    image_from_plot = np.roll(image_from_plot, -1,
                              axis=-1)  # Convert ARGB to RGBA

    # Fix alpha (white to alpha)
    # mask = np.sum(image_from_plot[:, :, :3], axis=2) == 3*255
    # image_from_plot[mask, 3] = 0
    mini = image_from_plot.min()
    image_from_plot[:, :, 3] = np.max(255 - image_from_plot[:, :, :3] + mini,
                                      axis=2)

    return image_from_plot
예제 #11
0
    def render(self, offset, window=None):
        self.fig = plt.figure()
        self.ax = self.fig.gca()
        self.ax.axis('off')
        canvas = FigureCanvasAgg(self.fig)

        plt.xlim([-160, 640])
        plt.ylim([460, -140])

        if window == None:
            window = self.window

        visible_objs = []
        for obj in self.beatmap["hitObjects"]:
            if (offset - window <= obj["startTime"] <= offset + window):
                visible_objs.append(obj)
        self.draw_objects(visible_objs, offset)

        canvas.draw()
        img = PIL.Image.frombytes('RGB', canvas.get_width_height(),
                                  canvas.tostring_rgb())

        plt.close(self.fig)

        return img
예제 #12
0
def to_array(fig):
    """
    Convert a matplotlib figure ``fig`` into a 3D numpy array.

    Example:

      >>> fig, ax = tfplot.subplots(figsize=(4, 4))
      >>> # draw whatever, e.g. ax.text(0.5, 0.5, "text")

      >>> im = to_array(fig)   # ndarray [288, 288, 4]

    Args:
      fig: A ``matplotlib.figure.Figure`` object.

    Returns:
      A numpy ``ndarray`` of shape ``(?, ?, 4)``, containing an RGB-A image of
      the figure.
    """

    # attach a new agg canvas
    _old_canvas = fig.canvas
    try:
        canvas = FigureCanvasAgg(fig)

        canvas.draw()
        w, h = canvas.get_width_height()

        img = np.frombuffer(canvas.tostring_argb(), dtype=np.uint8)
        img = img.reshape((h, w, 4))
        img = img[:, :, (1, 2, 3, 0)]  # argb -> rgba
        return img

    finally:
        # restore to the previous canvas, if any
        fig.set_canvas(_old_canvas)
예제 #13
0
def save_optimise_gif(param_p, param_q_list, loss_list, save_interval, x_grid,
                      save_path):
    os.makedirs(save_path, exist_ok=True)

    # import packages for GIF generation
    from PIL import Image
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

    print('*' * 60)
    print('generating optimiser gif at: %s' % save_path)
    print('*' * 60)

    landscape = get_energy_landscape(param_p, x_grid)

    p = Gaussian1D(x_grid, param_p[0, 0], param_p[0, 1])

    fig = plt.figure(figsize=(8, 4))
    canvas = FigureCanvas(fig)

    fig_list = []
    for i, (param_q, loss) in enumerate(zip(param_q_list, loss_list)):

        q = Gaussian1D(x_grid, param_q[0, 0], param_q[0, 1])
        plt.subplot(1, 2, 1)
        plt.plot(x_grid, landscape)
        plt.plot(param_q[0, 0], loss, 'k.', markersize=12)
        plt.grid()
        canvas.draw()

        plt.subplot(1, 2, 2)
        ax = plt.gca()
        plt.plot(x_grid, p, 'g')
        plt.plot(x_grid, q, 'r')
        plt.legend(['P(x)', 'Q(x)'])
        # plt.text(0.3, 0.9,'epoch: %d' % i * save_interval, ha='center', va='center', transform=ax.transAxes)

        plt.ylim([-0.01, 0.5])
        plt.grid()
        canvas.draw()

        plt.draw()

        # figure to image help from: https://stackoverflow.com/questions/35355930/matplotlib-figure-to-image-as-a-numpy-array
        fig_image = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
        fig_list.append(
            Image.fromarray(
                fig_image.reshape(canvas.get_width_height()[::-1] +
                                  (3, ))).resize((600, 300), Image.BILINEAR))
        plt.clf()

    # gif help from: https://note.nkmk.me/en/python-pillow-gif/
    fig_list[0].save(os.path.join(save_path, 'optimiser.gif'),
                     save_all=True,
                     append_images=fig_list[1:],
                     optimize=True,
                     duration=80,
                     loop=0)
    print('*' * 60)
    print(' ')
예제 #14
0
def fig2data(fig):
    canvas = FigureCanvasAgg(fig)
    canvas.draw()
    w, h = canvas.get_width_height()
    s = canvas.tostring_rgb()
    x = fromstring(s, uint8)
    x.shape = h, w, 3
    return x
예제 #15
0
def saveVaryingStdIntegralIntervalGif(folder='figures'):
    save_folder = os.path.join(folder, 'integral_intro', 'varystd')
    os.makedirs(save_folder, exist_ok=True)
    from PIL import Image
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

    print('*' * 60)
    print('Generating gif for varying SIGMA')
    print('*' * 60)
    x_grid = np.linspace(-6, 6, 100)

    fig = plt.figure(figsize=(8, 4))
    canvas = FigureCanvas(fig)
    mu = 0
    SIGMA = np.arange(0.5, 3, 0.1)

    fig_list = []
    for idx, sigma in enumerate(SIGMA):
        print('%d/%d' % (idx, SIGMA.shape[0]))
        q = Gaussian1D(x_grid, mu, sigma)

        plt.grid(True)
        ax = plt.gca()
        plt.plot(x_grid, q, 'b')
        x_grid_sel = x_grid[np.where(np.logical_and(x_grid >= -1,
                                                    x_grid <= 1))[0]]
        y_sel = Gaussian1D(x_grid_sel, mu, sigma)
        ax.fill_between(x_grid_sel, y_sel, 0, alpha=0.3, color='b')

        prob = discreteIntegral1D(y_sel, x_grid_sel[1] - x_grid_sel[0])

        plt.title('Probability of x=0 with +-1 tol is %0.2f' % prob)
        plt.ylim([-0.01, 0.9])
        # plt.title('Gaussian1D with $\mu=%0.1f$, $\sigma=%0.2f$' % (mu, sigma))
        canvas.draw()

        plt.savefig(os.path.join(save_folder,
                                 'gaussian_integral_int_sig_%0.5d.png' % idx),
                    dpi=100,
                    bbox_inches='tight')

        # figure to image help from: https://stackoverflow.com/questions/35355930/matplotlib-figure-to-image-as-a-numpy-array
        fig_image = np.frombuffer(canvas.tostring_rgb(), dtype='uint8')
        fig_list.append(
            Image.fromarray(
                fig_image.reshape(canvas.get_width_height()[::-1] + (3, ))))
        plt.clf()

    # gif help from: https://note.nkmk.me/en/python-pillow-gif/
    fig_list[0].save(os.path.join(save_folder,
                                  'gaussian_integral_int_sig.gif'),
                     save_all=True,
                     append_images=fig_list[1:],
                     optimize=False,
                     duration=100,
                     loop=0)
    print('*' * 60)
    print(' ')
예제 #16
0
def Histogram(inputImg, passThru=True, show=True):
    # plt.clf()
    newInputImg = cv2.cvtColor(inputImg, cv2.COLOR_BGR2RGB)
    # load image and split the image into the color channels
    b, g, r = cv2.split(newInputImg)

    # Creates a histogram of the different channels
    # fig = plt.figure()
    plt.hist(b.ravel(), 256, [0, 256])
    plt.hist(g.ravel(), 256, [0, 256])
    plt.hist(r.ravel(), 256, [0, 256])

    if passThru:
        BGR_Histogram = plt.show()
        return inputImg
    else:
        fig, axs = plt.subplots()
        # fig = Figure()
        canvas = FigureCanvas(fig)
        axs.hist(b.ravel(), 256, [0, 256])
        axs.hist(g.ravel(), 256, [0, 256])
        axs.hist(r.ravel(), 256, [0, 256])
        # axs.axis('off')
        fig.tight_layout(pad=0)

        # fig.add_axes(plt.hist(b.ravel(), 256, [0, 256]))
        # fig.add_a(plt.hist(g.ravel(), 256, [0, 256]))
        # fig.add_a(plt.hist(r.ravel(), 256, [0, 256]))
        # width, height = fig.get_size_inches(fig) * fig.get_dpi()
        # foo = canvas.get_width_height()[::-1] + (3,)

        canvas = FigureCanvas(fig)
        foo = canvas.get_width_height()[::-1] + (3, )
        print("foo", foo)
        canvas.draw()  # draw the canvas, cache the renderer
        s, (width, height) = canvas.print_to_buffer()
        print("(width, height)", (width, height))
        # time.sleep(.500)
        hh, ww = fig.get_size_inches()
        dpi = fig.get_dpi()

        print("dpi", dpi)

        # return np.fromstring(canvas.tostring_rgb(), dtype='uint8').reshape(height, width, 3)
        # img = np.fromstring(canvas.to_string_rgb(), dtype='uint8')
        # img.reshape(height, width, 3)

        s = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
        # time.sleep(.500)
        # print("stype:", type(s), s.shape)
        # imshow(s, "hist")
        fafafa = int(hh * dpi)
        fafafa2 = int(ww * dpi)
        s = s.reshape(height, width, 3)
        # time.sleep(.500)
        imshow(s, "hist")
        # print("stype:", type(s), s.shape)
        return s
예제 #17
0
 def _fig_to_array(self, fig):
     """
 Convert matplotlib figure to an image (numpy array)
 """
     fig.canvas.draw()  # Updates
     canvas = FigureCanvasAgg(fig)
     buf = canvas.tostring_rgb()
     w, h = canvas.get_width_height()
     return np.fromstring(buf, dtype=np.uint8).reshape(h, w, 3)
예제 #18
0
def plot_truth_and_posterior(gt_Xs, post_Xs, C, covs, num_pts=100):
  num_points = gt_Xs.shape[0]
  centered_gt_Xs = gt_Xs - np.mean(gt_Xs, axis=0)
  centered_post_Xs = post_Xs - np.mean(post_Xs, axis=0)
  R, scale = scipy.linalg.orthogonal_procrustes(centered_post_Xs, centered_gt_Xs)
  post_Xs = np.dot(centered_post_Xs, R)
  covs = np.matmul(R.T[np.newaxis,:,:], np.matmul(covs, R[np.newaxis,:,:]))
  gt_Xs = centered_gt_Xs

  gtb = window_for_points(gt_Xs)
  pb = window_for_points(post_Xs)

  fig = plt.figure(figsize=(8,4))
  canvas = FigureCanvas(fig)
  ax = [fig.add_subplot(1,2,1), fig.add_subplot(1,2,2)]
  ax[0].set_xlim(left=gtb[0][0], right=gtb[0][1])
  ax[0].set_ylim(bottom=gtb[1][0], top=gtb[1][1])
  ax[1].set_xlim(left=pb[0][0], right=pb[0][1])
  ax[1].set_ylim(bottom=pb[1][0], top=pb[1][1])

  x = np.arange(pb[0][0], stop=pb[0][1], step=(pb[0][1]-pb[0][0])/float(num_pts))[:num_pts]
  y = np.arange(pb[1][0], stop=pb[1][1], step=(pb[1][1]-pb[1][0])/float(num_pts))[:num_pts]
  X, Y = np.meshgrid(x, y)
  XY = np.reshape(np.stack([X,Y], axis=-1), [num_pts**2, 2])

  #batched_log_density = jax.jit(jax.vmap(util.mv_normal_logpdf, in_axes=(None, 0, 0)))
  #sum_density = lambda x: jnp.exp(jscipy.special.logsumexp(batched_log_density(x, post_Xs, Ls)))
  #batched_sum_density = jax.jit(jax.vmap(sum_density, in_axes=0))
  #density = np.reshape(batched_sum_density(XY), [num_pts, num_pts])
  #ax[1].imshow(density, extent=[pb[0][0], pb[0][1], pb[1][0], pb[1][1]], origin="lower")
  #ax[1].contour(X, Y, density, extent=[pb[0][0], pb[0][1], pb[1][0], pb[1][1]], origin="lower")

  for i in range(num_points):
    ax[0].plot(gt_Xs[i,0], gt_Xs[i,1], '.', color='darkorange')
    ax[0].annotate(i, (gt_Xs[i,0], gt_Xs[i,1]), color='white')
    ax[1].plot(post_Xs[i,0], post_Xs[i,1], '.', color='darkorange')
    ax[1].annotate(i, (post_Xs[i,0], post_Xs[i,1]), color='white')
    confidence_ellipses(post_Xs[i], covs[i], ax[1], color='deepskyblue')

  square_C = scipy.spatial.distance.squareform(C)
  for i in range(num_points):
    for j in range(i, num_points):
      if square_C[i,j] < 1.:
        ax[0].plot([gt_Xs[i,0], gt_Xs[j,0]], [gt_Xs[i,1], gt_Xs[j,1]], color='darkorange')
        ax[1].plot([post_Xs[i,0], post_Xs[j,0]], [post_Xs[i,1], post_Xs[j,1]], color='fuchsia')

  ax[0].set_facecolor('midnightblue')
  ax[1].set_facecolor('midnightblue')
  ax[0].set_title("Ground truth")
  ax[1].set_title("Posterior")

  fig.tight_layout(pad=0)
  canvas.draw()
  image_from_plot = np.frombuffer(canvas.tostring_rgb(), dtype=np.uint8)
  image_from_plot = image_from_plot.reshape(canvas.get_width_height()[::-1] + (3,))
  plt.close(fig)
  return image_from_plot[np.newaxis,:,:,:]
예제 #19
0
    def run(self):
        # -------- Main Program Loop -----------
        myfont = pygame.font.SysFont("monospace", 30)
        myfont.set_bold(True)
        fig = pylab.figure(
            figsize=[4, 4],
            dpi=100,
        )
        ax = fig.gca()
        self.diplayingPlot(ax)
        canvas = FigureCanvasAgg(fig)
        canvas.draw()

        size = canvas.get_width_height()
        while not self._done:
            # --- Main event loop
            for event in pygame.event.get():  # User did something
                if event.type == pygame.QUIT:  # If user clicked close
                    self._done = True  # Flag that we are done so we exit this loop

                elif event.type == pygame.VIDEORESIZE:  # window resized
                    self._screen = pygame.display.set_mode(
                        (1000, 400),
                        pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE,
                        32)
# --- Getting frames and drawing
            if self._kinect.has_new_depth_frame():
                frame = self._kinect.get_last_depth_frame()
                shape, x, y = self.draw_depth_frame(frame, self._frame_surface)
                frame = None

            self._screen.blit(self._frame_surface, (0, 0))
            try:
                point.remove()
            except:
                pass

            if x > 10 and y > 10:
                point = ax.scatter(x, y, color="Yellow")
            canvas.draw()
            renderer = canvas.get_renderer()
            raw_data = renderer.tostring_rgb()
            surf = pygame.image.fromstring(raw_data, size, "RGB")
            self._screen.blit(surf, (610, 0))
            label = myfont.render(shape, 1, (255, 255, 255))
            self._screen.blit(label, (0, 0))
            pygame.display.update()

            # --- Go ahead and update the screen with what we've drawn.
            pygame.display.flip()

            # --- Limit to 60 frames per second
            self._clock.tick(60)

# Close our Kinect sensor, close the window and quit.
        self._kinect.close()
        pygame.quit()
예제 #20
0
 def _fig_to_array(self, fig):
   """
   Convert matplotlib figure to an image (numpy array)
   """
   fig.canvas.draw()  # Updates
   canvas = FigureCanvasAgg(fig)
   buf = canvas.tostring_rgb()
   w, h = canvas.get_width_height()
   return np.fromstring(buf, dtype=np.uint8).reshape(h, w, 3)
예제 #21
0
def graph_image(graph):
    canvas = GraphCanvas(
        graph)  # https://matplotlib.org/gallery/api/agg_oo_sgskip.html

    canvas.draw()

    rgb_string = canvas.get_renderer().tostring_rgb()

    return pygame.image.fromstring(rgb_string, canvas.get_width_height(),
                                   "RGB")
예제 #22
0
def getRawImage(fig, for_vis=False):
    canvas = FigureCanvas(fig)
    canvas.draw()
    w, h = canvas.get_width_height()
    image = np.frombuffer(canvas.tostring_rgb(),
                          dtype='uint8').reshape([h, w, 3])
    if for_vis:
        image = np.swapaxes(image, 0, 2)
        image = np.swapaxes(image, 1, 2)
    return image
예제 #23
0
 def export_close_figure(self, fobject):
     """
     Export as a string and close the figure.
     """
     canvas = FigureCanvasAgg(fobject)
     canvas.draw()
     size, buf = canvas.get_width_height(), canvas.tostring_rgb()
     #close and return data
     close()
     return size, buf
예제 #24
0
def simulate(x, y, alpha, beta, gamma):
  img = Image.open('./public/population-density.png')
  width = img.size[0]//6
  height = img.size[1]//6
  img = img.resize((width, height)) 
  img = 255 - np.asarray(img)

  S_0 = img[:,:,1]
  I_0 = np.zeros_like(S_0)
  I_0[int(height * y), int(width * x)] = 1

  R_0 = np.zeros_like(S_0)

  T = 900                         # final time
  dt = 1                          # time increment
  N = int(T/dt) + 1               # number of time-steps
  t = np.linspace(0.0, T, N)      # time discretization

  # initialize the array containing the solution for each time-step
  u = np.empty((N, 3, S_0.shape[0], S_0.shape[1]))
  u[0][0] = S_0
  u[0][1] = I_0
  u[0][2] = R_0

  theCM = cm.get_cmap("Reds")
  theCM._init()
  alphas = np.abs(np.linspace(0, 1, theCM.N))
  theCM._lut[:-3,-1] = alphas


  # Compute data
  for n in range(N-1):
      u[n+1] = euler_step(u[n], f, dt, beta, gamma, alpha)

  keyFrames = []
  frames = 60.0 # upper limit on keyframes

  for i in range(0, N-1, int(N/frames)):
    fig = Figure()
    fig.figimage(img, resize=True)
    fig.figimage(u[i][1], vmin=0, cmap=theCM)

    canvas = FigureCanvas(fig)
    canvas.draw()

    rows, cols = canvas.get_width_height()

    keyFrames.append(np.fromstring(canvas.tostring_rgb(), dtype=np.uint8).reshape(cols, rows, 3))

  # Couldn't figure out a way to use BytesIO directly
  p = os.path.join(os.getenv('DATA_DIR'), "generated", uuid.uuid4().hex + ".mp4")

  imageio.mimsave(p, keyFrames, fps=5)

  return p
예제 #25
0
    def set_plot_state(self, extra_high=False, extra_low=False):
        """
        Build image state that wx.html understand
        by plotting, putting it into wx.FileSystem image object

        : extrap_high,extra_low: low/high extrapolations
        are possible extra-plots
        """
        # some imports
        import wx
        import matplotlib.pyplot as plt
        from matplotlib.backends.backend_agg import FigureCanvasAgg

        # we use simple plot, not plotpanel
        # make matlab figure
        fig = plt.figure()
        fig.set_facecolor('w')
        graph = fig.add_subplot(111)

        # data plot
        graph.errorbar(self.data.x, self.data.y, yerr=self.data.dy, fmt='o')
        # low Q extrapolation fit plot
        if not extra_low == 'False':
            graph.plot(self.theory_lowQ.x, self.theory_lowQ.y)
        # high Q extrapolation fit plot
        if not extra_high == 'False':
            graph.plot(self.theory_highQ.x, self.theory_highQ.y)
        graph.set_xscale("log", nonposx='clip')
        graph.set_yscale("log", nonposy='clip')
        graph.set_xlabel('$\\rm{Q}(\\AA^{-1})$', fontsize=12)
        graph.set_ylabel('$\\rm{Intensity}(cm^{-1})$', fontsize=12)
        canvas = FigureCanvasAgg(fig)
        # actually make image
        canvas.draw()

        # make python.Image object
        # size
        w, h = canvas.get_width_height()
        # convert to wx.Image
        wximg = wx.EmptyImage(w, h)
        # wxim.SetData(img.convert('RGB').tostring() )
        wximg.SetData(canvas.tostring_rgb())
        # get the dynamic image for the htmlwindow
        wximgbmp = wx.BitmapFromImage(wximg)
        # store the image in wx.FileSystem Object
        wx.FileSystem.AddHandler(wx.MemoryFSHandler())
        # use wx.MemoryFSHandler
        self.imgRAM = wx.MemoryFSHandler()
        # AddFile, image can be retrieved with 'memory:filename'
        self.imgRAM.AddFile('img_inv.png', wximgbmp, wx.BITMAP_TYPE_PNG)

        self.wximgbmp = 'memory:img_inv.png'
        self.image = fig
예제 #26
0
    def set_plot_state(self, extra_high=False, extra_low=False):
        """
        Build image state that wx.html understand
        by plotting, putting it into wx.FileSystem image object

        : extrap_high,extra_low: low/high extrapolations
        are possible extra-plots
        """
        # some imports
        import wx
        import matplotlib.pyplot as plt
        from matplotlib.backends.backend_agg import FigureCanvasAgg

        # we use simple plot, not plotpanel
        # make matlab figure
        fig = plt.figure()
        fig.set_facecolor('w')
        graph = fig.add_subplot(111)

        # data plot
        graph.errorbar(self.data.x, self.data.y, yerr=self.data.dy, fmt='o')
        # low Q extrapolation fit plot
        if not extra_low == 'False':
            graph.plot(self.theory_lowQ.x, self.theory_lowQ.y)
        # high Q extrapolation fit plot
        if not extra_high == 'False':
            graph.plot(self.theory_highQ.x, self.theory_highQ.y)
        graph.set_xscale("log", nonposx='clip')
        graph.set_yscale("log", nonposy='clip')
        graph.set_xlabel('$\\rm{Q}(\\AA^{-1})$', fontsize=12)
        graph.set_ylabel('$\\rm{Intensity}(cm^{-1})$', fontsize=12)
        canvas = FigureCanvasAgg(fig)
        # actually make image
        canvas.draw()

        # make python.Image object
        # size
        w, h = canvas.get_width_height()
        # convert to wx.Image
        wximg = wx.EmptyImage(w, h)
        # wxim.SetData(img.convert('RGB').tostring() )
        wximg.SetData(canvas.tostring_rgb())
        # get the dynamic image for the htmlwindow
        wximgbmp = wx.BitmapFromImage(wximg)
        # store the image in wx.FileSystem Object
        wx.FileSystem.AddHandler(wx.MemoryFSHandler())
        # use wx.MemoryFSHandler
        self.imgRAM = wx.MemoryFSHandler()
        # AddFile, image can be retrieved with 'memory:filename'
        self.imgRAM.AddFile('img_inv.png', wximgbmp, wx.BITMAP_TYPE_PNG)

        self.wximgbmp = 'memory:img_inv.png'
        self.image = fig
예제 #27
0
def figure_to_http(figure):
    """Stores a figure as bytes, and then returns an HttpResponse."""
    buffer = io.BytesIO()

    canvas = FigureCanvas(figure)
    canvas.draw()

    pil_image = PIL.Image.frombytes("RGB", canvas.get_width_height(),
                                    canvas.tostring_rgb())
    pil_image.save(buffer, "PNG")

    return HttpResponse(buffer.getvalue(), content_type="image/png")
예제 #28
0
    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()
예제 #29
0
def mat_to_img(mat: np.array) -> np.array:
    fig = Figure()
    canvas = FigureCanvas(fig)
    w, h = canvas.get_width_height()
    ax = fig.add_subplot(111)
    ax.matshow(mat)

    canvas.draw()  # draw the canvas, cache the renderer

    image = np.fromstring(canvas.tostring_rgb(), dtype='uint8')
    image.shape = (h, w, 3)
    image = np.moveaxis(np.moveaxis(image, 1, -1), 1, 0)
    return image
예제 #30
0
def plot_spectrum(real, fake):
    x = np.linspace(1, 31, 31, endpoint=True)
    fig = Figure()
    canvas = FigureCanvasAgg(fig)
    ax = fig.gca()
    plot_real,  = ax.plot(x, real)
    plot_fake,  = ax.plot(x, fake)
    fig.legend((plot_real,plot_fake), ('real', 'fake'))
    canvas.draw()
    I = np.fromstring(canvas.tostring_rgb(), dtype='uint8', sep='')
    I = I.reshape(canvas.get_width_height()[::-1]+(3,))
    I = np.transpose(I, [2,0,1])
    return np.float32(I)
예제 #31
0
def to_summary(fig, tag):
    """
    Convert a matplotlib figure ``fig`` into a TensorFlow Summary object
    that can be directly fed into ``Summary.FileWriter``.

    Example:

      >>> fig, ax = ...    # (as above)
      >>> summary = to_summary(fig, tag='MyFigure/image')

      >>> type(summary)
      tensorflow.core.framework.summary_pb2.Summary
      >>> summary_writer.add_summary(summary, global_step=global_step)

    Args:
      fig: A ``matplotlib.figure.Figure`` object.
      tag (string): The tag name of the created summary.

    Returns:
      A TensorFlow ``Summary`` protobuf object containing the plot image
      as a image summary.
    """
    if not isinstance(tag, six.string_types):
        raise TypeError("tag must be a string type")

    # attach a new agg canvas
    _old_canvas = fig.canvas
    try:
        canvas = FigureCanvasAgg(fig)

        canvas.draw()
        w, h = canvas.get_width_height()

        # get PNG data from the figure
        png_buffer = BytesIO()
        canvas.print_png(png_buffer)
        png_encoded = png_buffer.getvalue()
        png_buffer.close()

        summary_image = Summary.Image(
            height=h,
            width=w,
            colorspace=4,  # RGB-A
            encoded_image_string=png_encoded)
        summary = Summary(value=[Summary.Value(tag=tag, image=summary_image)])
        return summary

    finally:
        fig.canvas = _old_canvas
예제 #32
0
def plot_posterior(Xs, i, C, L, num_pts=100):
  num_points = Xs.shape[0]
  xb, yb= window_for_points(Xs)
  fig = plt.figure(figsize=(8,4))
  canvas = FigureCanvas(fig)
  ax = [fig.add_subplot(1,2,1), fig.add_subplot(1,2,2)]
  ax[0].set_xlim(left=xb[0], right=xb[1])
  ax[0].set_ylim(bottom=yb[0], top=yb[1])
  ax[1].set_xlim(left=xb[0], right=xb[1])
  ax[1].set_ylim(bottom=yb[0], top=yb[1])

  x = np.arange(xb[0], stop=xb[1], step=(xb[1]-xb[0])/float(num_pts))[:num_pts]
  y = np.arange(yb[0], stop=yb[1], step=(yb[1]-yb[0])/float(num_pts))[:num_pts]
  X, Y = np.meshgrid(x, y)
  XY = np.reshape(np.stack([X,Y], axis=-1), [num_pts**2, 2])

  log_density = jax.vmap(lambda x: util.mv_normal_logpdf(x, Xs[i], L))
  log_density_im = np.reshape(log_density(XY), [num_pts, num_pts])
  density_im = np.exp(log_density_im)

  ax[0].imshow(log_density_im, extent=[xb[0], xb[1], yb[0], yb[1]], origin="lower")
  ax[0].contour(X, Y, log_density_im, extent=[xb[0], xb[1], yb[0], yb[1]], origin="lower")
  ax[0].set_title("Log Posterior over point %d" % i)

  ax[1].imshow(density_im, extent=[xb[0], xb[1], yb[0], yb[1]], origin="lower")
  ax[1].contour(X, Y, density_im, extent=[xb[0], xb[1], yb[0], yb[1]], origin="lower")
  ax[1].set_title("Posterior over point %d" % i)

  for i in range(num_points):
    ax[0].plot(Xs[i,0], Xs[i,1], 'o', color='fuchsia')
    ax[0].annotate(i, (Xs[i,0], Xs[i,1]), color='white')
    ax[1].plot(Xs[i,0], Xs[i,1], 'o', color='fuchsia')
    ax[1].annotate(i, (Xs[i,0], Xs[i,1]), color='white')

  square_C = scipy.spatial.distance.squareform(C)
  for i in range(num_points):
    for j in range(i, num_points):
      if square_C[i,j] < 1.:
        ax[0].plot([Xs[i,0], Xs[j,0]], [Xs[i,1], Xs[j,1]], color='fuchsia')
        ax[1].plot([Xs[i,0], Xs[j,0]], [Xs[i,1], Xs[j,1]], color='fuchsia')

  fig.tight_layout(pad=0)
  canvas.draw()
  image_from_plot = np.frombuffer(canvas.tostring_rgb(), dtype=np.uint8)
  image_from_plot = image_from_plot.reshape(canvas.get_width_height()[::-1] + (3,))
  plt.close(fig)
  return image_from_plot[np.newaxis,:,:,:]
예제 #33
0
    def _animate(self, frames, func, **animArgs):
        if self._outputParam is None: raise NotImplemented

        images = []
        figure = plt.Figure(figsize=(6,6), dpi=300, facecolor='w')
        axes = figure.add_subplot(111)
        canvas = FigureCanvasAgg(figure) 

        for frame in frames:
            axes.clear()
            func(frame, axes)
            canvas.draw()
            
            image = np.fromstring(canvas.tostring_rgb(), dtype=np.uint8)
            image.shape = canvas.get_width_height() + (3,)
            images.append(image)
        self._render(images)
예제 #34
0
    def _matplotlib_fig_arr(fig):
        """
        Convert a Matplotlib figure to a 3D numpy array with RGB channels and return it.
        fig - a matplotlib figure
        """
        try:
            from matplotlib.backends.backend_agg import FigureCanvasAgg
        except ImportError:
            return None

        canvas = FigureCanvasAgg(fig)
        canvas.draw()
        w, h = canvas.get_width_height()
        buf = mdp.numx.fromstring(canvas.tostring_rgb(), dtype='uint8')
        buf.shape = (h, w, 3)

        return buf
예제 #35
0
    def get_image(self):
        width = self.extent[1] - self.extent[0]
        height = self.extent[3] - self.extent[2]
        self.figure.set_size_inches((6, 6. * width / height))
        self.figure.set_dpi(self.settings.exportDpi)
        self.figure.patch.set_alpha(0)
        self.axes.axesPatch.set_alpha(0)
        canvas = FigureCanvasAgg(self.figure)
        canvas.draw()

        renderer = canvas.get_renderer()
        if matplotlib.__version__ >= '1.2':
            buf = renderer.buffer_rgba()
        else:
            buf = renderer.buffer_rgba(0, 0)
        size = canvas.get_width_height()
        image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)

        return image
예제 #36
0
def figure_to_pixmap(figure):
    """Get a pixmap from a headless MPL figure
    :param figure: a headless (or any other) matplotlib figure object
    :type filename: matplotlib.figure.Figure
    :returns: 3D numpy uint8 array of pixel values (the image)
    """
    canvas = FigureCanvasAgg(figure)
    canvas.draw()
    w, h = canvas.get_width_height()
    pixmap_string = canvas.tostring_argb()
    #line up the channels right
    pix_array = np.fromstring(pixmap_string, dtype=np.uint8)
    pix_array.shape = (w, h, 4)
    # pix_array = pix_array[:, :, ::-1]
    swapped_array = np.zeros(pix_array.shape, dtype=np.uint8)
    swapped_array[:,:,3] = pix_array[:,:,0]
    swapped_array[:,:,0] = pix_array[:,:,1]
    swapped_array[:,:,1] = pix_array[:,:,2]
    swapped_array[:,:,2] = pix_array[:,:,3]
    return swapped_array
예제 #37
0
def export_image(filename, format, figure):
    oldSize = figure.get_size_inches()
    oldDpi = figure.get_dpi()
    figure.set_size_inches((8, 4.5))
    figure.set_dpi(600)

    canvas = FigureCanvasAgg(figure)
    canvas.draw()
    renderer = canvas.get_renderer()
    if matplotlib.__version__ >= '1.2':
        buf = renderer.buffer_rgba()
    else:
        buf = renderer.buffer_rgba(0, 0)
    size = canvas.get_width_height()
    image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
    image = image.convert('RGB')
    ext = File.get_export_ext(format, File.Exports.IMAGE)
    image.save(filename, format=ext[1::])

    figure.set_size_inches(oldSize)
    figure.set_dpi(oldDpi)
예제 #38
0
    def _animate(self, frames, func, **animArgs):
        if self._outputParam is None:
            raise NotImplemented
        figure = plt.Figure(figsize=(6, 6), dpi=300, facecolor="w")
        axes = figure.add_subplot(111)
        canvas = FigureCanvasAgg(figure)
        chunks = 10
        multiframes = [frames[i : i + chunks] for i in xrange(0, len(frames), chunks)]
        count = 0
        for frames in multiframes:
            count += 1
            images = []
            for frame in frames:
                axes.clear()
                func(frame, axes)
                canvas.draw()

                image = np.fromstring(canvas.tostring_rgb(), dtype=np.uint8)
                image.shape = canvas.get_width_height() + (3,)
                images.append(image)
            self._render(images, num=str(count))
        if count > 1:
            self._joinParts(count)
예제 #39
0
    def __draw_image(self, sizeInches, ppi):
        oldSize = self.figure.get_size_inches()
        oldDpi = self.figure.get_dpi()
        self.figure.set_size_inches(sizeInches)
        self.figure.set_dpi(ppi)

        canvas = FigureCanvasAgg(self.figure)
        canvas.draw()
        renderer = canvas.get_renderer()
        if matplotlib.__version__ >= '1.2':
            buf = renderer.buffer_rgba()
        else:
            buf = renderer.buffer_rgba(0, 0)
        size = canvas.get_width_height()
        image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)

        self.figure.set_size_inches(oldSize)
        self.figure.set_dpi(oldDpi)

        imageWx = wx.EmptyImage(image.size[0], image.size[1])
        imageWx.SetData(image.convert('RGB').tostring())

        return imageWx
예제 #40
0
 def gen_land_bitmap(bmap, resolution_meters):
             
     #Get land polygons and bbox of polygons
     polys = []
     xmin = np.finfo(np.float64).max
     xmax = -np.finfo(np.float64).max
     ymin = xmin
     ymax = xmax
             
     logging.debug('Rasterizing Basemap, number of land polys: ' + str(len(bmap.landpolygons)))
     # If no polys: return a zero map
     if (len(bmap.landpolygons) == 0):
         raise Exception('Basemap contains no land polys to rasterize')
     
     for polygon in bmap.landpolygons:
         coords = polygon.get_coords()
         xmin = min(xmin, np.min(coords[:,0]))
         xmax = max(xmax, np.max(coords[:,0]))
         ymin = min(ymin, np.min(coords[:,1]))
         ymax = max(ymax, np.max(coords[:,1]))
         polys.append(coords)
         
     xmin = np.floor(xmin/resolution_meters)*resolution_meters
     xmax = np.ceil(xmax/resolution_meters)*resolution_meters
     ymin = np.floor(ymin/resolution_meters)*resolution_meters
     ymax = np.ceil(ymax/resolution_meters)*resolution_meters
     
     # For debugging
     logging.debug('Rasterizing Basemap, bounding box: ' + str([xmin, xmax, ymin, ymax]))
     
     # Switch backend to prevent creating an empty figure in notebook
     orig_backend = plt.get_backend()
     plt.switch_backend('agg')
     
     # Create figure to help rasterize
     fig = plt.figure(frameon=False)
     ax = plt.Axes(fig, [0., 0., 1., 1.])
     ax.set_axis_off()
     fig.add_axes(ax)   
     ax.set_xlim(xmin, xmax)
     ax.set_ylim(ymin, ymax)
     
     # Set aspect and resolution
     # Aspect gives 1 in high plot
     aspect = (xmax-xmin)/(ymax-ymin)
     resolution_dpi = (ymax-ymin) / resolution_meters
     
     fig.set_dpi(resolution_dpi)
     fig.set_size_inches(aspect, 1)
     
     # Add polygons
     lc = PolyCollection(polys, facecolor='k', lw=0)
     ax.add_collection(lc)
     
     # Create canvas and rasterize
     canvas = FigureCanvasAgg(fig)
     try:
         canvas.draw()
         width, height = canvas.get_width_height()
         rgb_data = np.fromstring(canvas.tostring_rgb(), dtype='uint8').reshape(height, width, 3)
         data = rgb_data[:,:,1]
         plt.close(fig) #comment this for debugging purposes and replace with plt.show()
         logging.debug('Rasterized size: ' + str([width, height]))
     except MemoryError:
         gc.collect()
         raise Exception('Basemap rasterized size too large: ' 
                         + str(aspect*resolution_dpi) + '*' + str(resolution_dpi) 
                         + ' cells')
     finally:
         # Reset backend
         plt.switch_backend(orig_backend)
     
     
     return RasterizedBasemap(xmin, xmax, ymin, ymax, resolution_meters, data)
예제 #41
0
while disp.isNotDone():
    ax = fig.gca(projection='3d')
    ax.set_xlabel('BLUE', color=(0,0,1) )
    ax.set_ylabel('GREEN',color=(0,1,0))
    ax.set_zlabel('RED',color=(1,0,0))
    # Get the color histogram
    img = cam.getImage().scale(0.3)
    rgb = img.getNumpyCv2()
    hist = cv2.calcHist([rgb],[0,1,2],None,[bins,bins,bins],[0,256,0,256,0,256])
    hist = hist/np.max(hist)
    # render everything
    [ ax.plot([x],[y],[z],'.',markersize=max(hist[x,y,z]*100,6),color=color) for x,y,z,color in idxs if(hist[x][y][z]>0) ]
    #[ ax.plot([x],[y],[z],'.',color=color) for x,y,z,color in idxs if(hist[x][y][z]>0) ]
    ax.set_xlim3d(0, bins-1)
    ax.set_ylim3d(0, bins-1)
    ax.set_zlim3d(0, bins-1)
    azim = (azim+0.5)%360
    ax.view_init(elev=35, azim=azim)
    ########### convert matplotlib to  SimpleCV image
    canvas.draw()
    renderer = canvas.get_renderer()
    raw_data = renderer.tostring_rgb()
    size = canvas.get_width_height()    
    surf = pg.image.fromstring(raw_data, size, "RGB")
    figure = Image(surf)
    ############ All done
    figure = figure.floodFill((0,0), tolerance=5,color=Color.WHITE)
    result = figure.blit(img, pos=(20,20))
    result.save(disp)
    fig.clf()