在PyGame中Blitting TIFF阵列非常慢

时间:2016-06-14 19:03:59

标签: python image-processing pygame tiff blit

编辑:结果问题是我反复使用tifffile方法检索图像数组;我把它作为一个类属性,一切顺利!

我是一名非常新手的程序员,致力于为血管图像数据制作开源浏览器,我们的显微镜将其保存为多页TIFF。经过一段时间的搜索,我决定将PyGame作为后端,因为我之前已经使用过它,这似乎非常适合轻松操作和查看图像。我已经添加了滚动堆栈中图像的功能,其中大部分是通过调用以下方法完成的:

def view_slice(self, background, z):
    """
    Update the view of 'background' to display
    the image at depth 'z'.

    NOTE: This is currently the slowest part of the program, taking
    about ~0.3 seconds. Will try to optimize before adding more
    functionality.

    :param background: The Surface of the current image.
    :param z: The depth of the image to view.

    :type background: pygame.Surface
    :type z: int

    :rtype: None
    """
    start_time = time.time()
    imarray = self.stack.getarray[z]

    # Check if window has been resized. If so, resize
    # next image to current window size.
    if imarray.shape != background.get_size():
        bgsurf = pg.Surface(imarray.shape, depth=BIT_DEPTH)
        pg.surfarray.blit_array(bgsurf, imarray)
        self.orig_bg = bgsurf
        bgsurf = pg.transform.scale(bgsurf, background.get_size())
        self.screen.blit(bgsurf, (0, 0))
        self.curr_bg = bgsurf
    else:
        pg.surfarray.blit_array(background, imarray)
        self.screen.blit(background, (0, 0))
    self.current_slice = z
    print "--- {0} seconds ---".format(time.time() - start_time)

目前,我将每个图像的numpy数组放到屏幕上,调整大小以适应窗口,但此代码非常慢。在PyGame中有没有更快的方法呢?我不确定是否有更便宜的方式来执行此操作;如果我忽视任何事情,请告诉我。

整个代码在这里有点展示,但是如果你想看一下它,它就在my GitHub page上。上面的代码段在viewer.py。

我已经放置了样本TIFF堆栈here

谢谢! :)

0 个答案:

没有答案