importunittestimportpygame,pygame.transformfrompygame.localsimport*defshow_image(s,images=[]):#pygame.display.init()size=s.get_rect()[2:]screen=pygame.display.set_mode(size)screen.blit(s,(0,0))pygame.display.flip()pygame.event.pump()going=Trueidx=0whilegoing:events=pygame.event.get()foreinevents:ife.type==QUIT:going=Falseife.type==KEYDOWN:ife.keyin[K_s,K_a]:ife.key==K_s:idx+=1ife.key==K_a:idx-=1s=images[idx]screen.blit(s,(0,0))pygame.display.flip()pygame.event.pump()else:going=Falsepygame.display.quit()pygame.display.init()defthreshold(return_surf,surf,color,threshold=(0,0,0),diff_color=(0,0,0),change_return=True):""" given the color it makes return_surf only have areas with the given colour. """width,height=surf.get_width(),surf.get_height()ifchange_return:return_surf.fill(diff_color)try:r,g,b=colorexceptValueError:r,g,b,a=colortry:tr,tg,tb=colorexceptValueError:tr,tg,tb,ta=colorsimilar=0foryinxrange(height):forxinxrange(width):c1=surf.get_at((x,y))if((abs(c1[0]-r)<tr)&(abs(c1[1]-g)<tg)&(abs(c1[2]-b)<tb)):# this pixel is within the threshold.ifchange_return:return_surf.set_at((x,y),c1)similar+=1#else:# print c1, c2returnsimilarclassTransformTest(unittest.TestCase):deftest_scale_alpha(self):""" see if set_alpha information is kept. """s=pygame.Surface((32,32))s.set_alpha(55)self.assertEqual(s.get_alpha(),55)s=pygame.Surface((32,32))s.set_alpha(55)s2=pygame.transform.scale(s,(64,64))s3=s.copy()self.assertEqual(s.get_alpha(),s3.get_alpha())self.assertEqual(s.get_alpha(),s2.get_alpha())deftest_scale_destination(self):""" see if the destination surface can be passed in to use. """s=pygame.Surface((32,32))s2=pygame.transform.scale(s,(64,64))s3=s2.copy()s3=pygame.transform.scale(s,(64,64),s3)pygame.transform.scale(s,(64,64),s2)# the wrong size surface is past in. Should raise an error.self.assertRaises(ValueError,pygame.transform.scale,s,(33,64),s3)if1:s=pygame.Surface((32,32))s2=pygame.transform.smoothscale(s,(64,64))s3=s2.copy()s3=pygame.transform.smoothscale(s,(64,64),s3)pygame.transform.smoothscale(s,(64,64),s2)# the wrong size surface is past in. Should raise an error.self.assertRaises(ValueError,pygame.transform.smoothscale,s,(33,64),s3)deftest_threshold_surface(self):""" """#pygame.transform.threshold(DestSurface, Surface, color, threshold = (0,0,0,0), diff_color = (0,0,0,0), change_return = True): return num_threshold_pixelsthreshold=pygame.transform.thresholds1=pygame.Surface((32,32))s2=pygame.Surface((32,32))s3=pygame.Surface((1,1))s1.fill((40,40,40))s2.fill((255,255,255))num_threshold_pixels=threshold(s2,s1,(30,30,30),(11,11,11),(255,0,0),True)#num_threshold_pixels = threshold(s2, s1, (30,30,30))self.assertEqual(num_threshold_pixels,s1.get_height()*s1.get_width())self.assertEqual(s2.get_at((0,0)),(40,40,40,255))if1:# only one pixel should not be changed.s1.fill((40,40,40))s2.fill((255,255,255))s1.set_at((0,0),(170,170,170))num_threshold_pixels=threshold(s2,s1,(30,30,30),(11,11,11),(0,0,0),True)#num_threshold_pixels = threshold(s2, s1, (30,30,30))self.assertEqual(num_threshold_pixels,(s1.get_height()*s1.get_width())-1)self.assertEqual(s2.get_at((0,0)),(0,0,0,255))self.assertEqual(s2.get_at((0,1)),(40,40,40,255))self.assertEqual(s2.get_at((17,1)),(40,40,40,255))# abs(40 - 255) < 100#(abs(c1[0] - r) < tr)if1:s1.fill((160,160,160))s2.fill((255,255,255))num_threshold_pixels=threshold(s2,s1,(255,255,255),(100,100,100),(0,0,0),True)self.assertEqual(num_threshold_pixels,(s1.get_height()*s1.get_width()))if1:# only one pixel should not be changed.s1.fill((40,40,40))s2.fill((255,255,255))s1.set_at((0,0),(170,170,170))num_threshold_pixels=threshold(s3,s1,(30,30,30),(11,11,11),(0,0,0),False)#num_threshold_pixels = threshold(s2, s1, (30,30,30))self.assertEqual(num_threshold_pixels,(s1.get_height()*s1.get_width())-1)deftest_laplacian(self):""" """SIZE=32s1=pygame.Surface((SIZE,SIZE))s2=pygame.Surface((SIZE,SIZE))s1.fill((10,10,70))pygame.draw.line(s1,(255,0,0),(3,10),(20,20))# a line at the last row of the image.pygame.draw.line(s1,(255,0,0),(0,31),(31,31))pygame.transform.laplacian(s1,s2)#show_image(s1)#show_image(s2)self.assertEqual(s2.get_at((0,0)),(0,0,0,255))self.assertEqual(s2.get_at((3,10)),(255,0,0,255))self.assertEqual(s2.get_at((0,31)),(255,0,0,255))self.assertEqual(s2.get_at((31,31)),(255,0,0,255))# here we create the return surface.s2=pygame.transform.laplacian(s1)self.assertEqual(s2.get_at((0,0)),(0,0,0,255))self.assertEqual(s2.get_at((3,10)),(255,0,0,255))self.assertEqual(s2.get_at((0,31)),(255,0,0,255))self.assertEqual(s2.get_at((31,31)),(255,0,0,255))deftest_average_surfaces(self):""" """SIZE=32s1=pygame.Surface((SIZE,SIZE))s2=pygame.Surface((SIZE,SIZE))s3=pygame.Surface((SIZE,SIZE))s1.fill((10,10,70))s2.fill((10,20,70))s3.fill((10,130,10))surfaces=[s1,s2,s3]surfaces=[s1,s2]sr=pygame.transform.average_surfaces(surfaces)self.assertEqual(sr.get_at((0,0)),(10,15,70,255))self.assertRaises(TypeError,pygame.transform.average_surfaces,1)self.assertRaises(TypeError,pygame.transform.average_surfaces,[])self.assertRaises(TypeError,pygame.transform.average_surfaces,[1])self.assertRaises(TypeError,pygame.transform.average_surfaces,[s1,1])self.assertRaises(TypeError,pygame.transform.average_surfaces,[1,s1])self.assertRaises(TypeError,pygame.transform.average_surfaces,[s1,s2,1])self.assertRaises(TypeError,pygame.transform.average_surfaces,(sforsin[s1,s2,s3]))if__name__=='__main__':unittest.main()