[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Issue with pygame.mask
Hi,
I am using pygame.mask.from_surface(Surface, color, threshold = (0,0,0,255)) to convert a surface into a 2D binary mask.
I expected the default threshold (0,0,0) of this function to have it match only that the exact color specified (ie 
within 0 values of the color). However, it seems that it instead matches NO pixels. To get the behaviour I expected I 
needed to specify a threshold of (1, 1, 1, 255)
Is this a bug, or are my expectations wrong?
Regards,
Peter Finlayson
import pygame
def main():
    pygame.init()
    pygame.display.set_mode((800, 600))
    surf = pygame.Surface((20, 20))
    surf.fill((255, 255, 255))
    pos = (10, 10)
    color = surf.get_at(pos)
    print("Pixel at %s is %s" % (str(pos), str(color)))
    threshold = (0, 0, 0, 255)  # the default threshold
    mask = pygame.mask.from_threshold(surf, color, threshold)
    print("Mask has %d pixels of colour %s set with a threshhold %s" %
          (mask.count(), str(color), str(threshold)))
    threshold = (1, 1, 1, 255)
    mask = pygame.mask.from_threshold(surf, color, threshold)
    print("Mask has %d pixels of colour %s set with a threshhold %s" %
          (mask.count(), str(color), str(threshold)))
    threshold = (2, 2, 2, 255)
    color = (254, 254, 254, 255)
    mask = pygame.mask.from_threshold(surf, color, threshold)
    print("Mask has %d pixels of colour %s set with a threshhold %s" %
          (mask.count(), str(color), str(threshold)))
if __name__ == "__main__":
    try:
        main()
    finally:
        pygame.quit()