如何使用pygame反转阈值图像?

时间:2014-12-23 17:16:16

标签: python pygame threshold invert

我无法找到真正的方法来反转结果图像。我试图在pygame.transform.threshold方法中使用inverse = True参数,但它会导致错误。换句话说,我需要采取反向阈值。

def threshold_image(self, image):

    """Applies a threshold to an image and returns the thresholded image

    arguments
    image           --  the image that should be thresholded, a
                        pygame.surface.Surface instance

    returns
    thresholded     --  the thresholded image, a
                        pygame.surface.Surface instance
    """

    # surface to apply threshold to surface
    thimg = pygame.surface.Surface(self.get_size(), 0, image)

    # perform thresholding
    th = (self.settings['threshold'], self.settings['threshold'],
          self.settings['threshold'])
    pygame.transform.threshold(thimg, image, self.settings['pupilcol'], th,
                               self.settings['nonthresholdcol'], 1)

    return thimg

1 个答案:

答案 0 :(得分:0)

我不确定,但在return thimg语句之前添加了一行的内容应该会反转您获得所需的结果:

- 未经测试 -

def threshold_image(self, image):

    """Applies a threshold to an image and returns the thresholded image

    arguments
    image           --  the image that should be thresholded, a
                        pygame.surface.Surface instance

    returns
    thresholded     --  the thresholded image, a
                        pygame.surface.Surface instance
    """

    # surface to apply threshold to surface
    thimg = pygame.surface.Surface(self.get_size(), 0, image)

    # perform thresholding
    th = (self.settings['threshold'], self.settings['threshold'],
          self.settings['threshold'])
    pygame.transform.threshold(thimg, image, self.settings['pupilcol'], th,
                               self.settings['nonthresholdcol'], 1)

    # Additional statements to invert results
    inv = pygame.Surface(thimg.get_rect().size)
    inv.fill((255,255,255))
    inv.blit(thimg, (0,0), None, pygame.BLEND_RGB_SUB)

    return thimg