def createCharTexture( self, char, mode=None ): """Create character's texture/bitmap as a Numeric array w/ width and height This uses PyGame and Numeric to attempt to create an anti-aliased luminance texture-map of the given character and return it as (data, width, height). """ try: letter_render = self.font.render( char, 1, (255,255,255) ) except: traceback.print_exc() return None, font.CharacterMetrics(char,0,0) else: # XXX Figure out why this rotate appears to be required :( letter_render = transform.rotate( letter_render, -90.0) colour = surfarray.array3d( letter_render ) alpha = surfarray.array_alpha( letter_render ) colour[:,:,1] = alpha colour = colour[:,:,:2] colour = contiguous( colour ) # This produces what looks like garbage, but displays correctly colour.shape = (colour.shape[1],colour.shape[0],)+colour.shape[2:] return colour, font.CharacterMetrics( char, colour.shape[0], colour.shape[1], )
def createCharTexture(self, char, mode=None): """Create character's texture/bitmap as a Numeric array w/ width and height This uses PyGame and Numeric to attempt to create an anti-aliased luminance texture-map of the given character and return it as (data, width, height). """ try: letter_render = self.font.render(char, 1, (255, 255, 255)) except: traceback.print_exc() return None, font.CharacterMetrics(char, 0, 0) else: # XXX Figure out why this rotate appears to be required :( letter_render = transform.rotate(letter_render, -90.0) colour = surfarray.array3d(letter_render) alpha = surfarray.array_alpha(letter_render) colour[:, :, 1] = alpha colour = colour[:, :, :2] colour = contiguous(colour).astype('B') # This produces what looks like garbage, but displays correctly colour.shape = ( colour.shape[1], colour.shape[0], ) + colour.shape[2:] return colour, font.CharacterMetrics( char, colour.shape[0], colour.shape[1], )
def runTest(self) -> None: width, height = 800, 600 screen = pygame.display.set_mode((width * 2, height)) pygame.display.set_caption("Blur5x5Array32") checker = pygame.image.load( '../Assets/background_checker.png').convert() checker = pygame.transform.smoothscale(checker, (1600, 600)) background = pygame.image.load('../Assets/Aliens.jpg').convert_alpha() background = pygame.transform.smoothscale(background, (800, 600)) # Basic checks blurring an array with only 255 values # Full array with 255 values full_255 = numpy.full((800, 600, 4), (255, 255, 255, 0), numpy.uint8) blur_array = blur5x5_array32(full_255) self.assertTrue(numpy.array_equal(blur_array, full_255)) # Basic checks blurring an array with only 0 values full_0 = numpy.full((800, 600, 4), (0, 0, 0, 0), numpy.uint8) blur_array = blur5x5_array32(full_0) self.assertTrue(numpy.array_equal(blur_array, full_0)) self.assertEqual(background.get_bytesize(), 4) rgba_array = array3d(background) blur_rgba_array = blur5x5_array32(rgba_array) self.assertIsInstance(blur_rgba_array, numpy.ndarray) background = pygame.image.load('../Assets/Aliens.jpg').convert_alpha() background = pygame.transform.smoothscale(background, (800, 600)) alpha_array = array_alpha(background) rgb_array = array3d(background) rgba_array = numpy.dstack((rgb_array, alpha_array)).transpose(1, 0, 2) blur_rgba_array = blur5x5_array32(rgba_array) w, h, d = blur_rgba_array.shape # Check if the array kept the same dimensions width and height # Check also that the alpha channel as been removed self.assertTrue(d == 4) self.assertTrue(w == 600) self.assertTrue(h == 800) self.assertIsInstance(blur_rgba_array, numpy.ndarray) blur_surface = pygame.image.frombuffer(blur_rgba_array, (h, w), 'RGBA') # Array shape (w, h, 4) uint8 rgba_array = numpy.zeros((800, 600, 4), numpy.uint8) blur5x5_array32(rgba_array) # Testing wrong datatype rgba_array = numpy.zeros((800, 600, 4), numpy.float32) self.assertRaises(ValueError, blur5x5_array32, rgba_array) # Testing wrong datatype rgba_array = numpy.zeros((800, 600, 4), numpy.int8) self.assertRaises(ValueError, blur5x5_array32, rgba_array) display(screen, checker, background, blur_surface)
def getBitmap(upperPipes,lowerPipes,base): obstacleSurface = pygame.Surface((SCREENWIDTH, SCREENHEIGHT),flags=pygame.SRCALPHA) obstacleSurface.fill((0,0,0,0)) #Add obstacles for uPipe, lPipe in zip(upperPipes, lowerPipes): obstacleSurface.blit(IMAGES['pipe'][0], (uPipe['x'], uPipe['y'])) obstacleSurface.blit(IMAGES['pipe'][1], (lPipe['x'], lPipe['y'])) obstacleSurface.blit(IMAGES['base'], base) #Copy alpha values #bitmap = pygame.mask.from_surface(obstacleSurface,255) bitmap = surfarray.array_alpha(obstacleSurface) return bitmap
background = pygame.image.load('../Assets/Aliens.jpg').convert_alpha() background = pygame.transform.smoothscale(background, (1280, 1024)) rgb_array = pixels3d(background) print('\nTESTING WITH IMAGE 1280x1024') N = 10 t = timeit.timeit("blur5x5_array24(rgb_array)", "from __main__ import blur5x5_array24, rgb_array", number=N) print( "\nPerformance testing blur5x5_array24 per call %s overall time %s for %s" % (round(float(t) / float(N), 10), round(float(t), 5), N)) alpha_array = array_alpha(background) rgb_array = array3d(background) rgba_array = numpy.dstack((rgb_array, alpha_array)).transpose(1, 0, 2) t = timeit.timeit("blur5x5_array32(rgb_array)", "from __main__ import blur5x5_array32, rgb_array", number=N) print( "\nPerformance testing blur5x5_array32 per call %s overall time %s for %s" % (round(float(t) / float(N), 10), round(float(t), 5), N)) alpha_array = pixels_alpha(background) rgb_array = pixels3d(background) rgba_array = numpy.dstack((rgb_array, alpha_array)).transpose(1, 0, 2) t = timeit.timeit("blur5x5_array32_inplace(rgba_array)", "from __main__ import blur5x5_array32_inplace, rgba_array",
def circleMask(r): w = h = r * 2 + 1 sf = pg.Surface((w, h), flags=pg.SRCALPHA) gfx.filled_circle(sf, r, r, r, (0, 0, 0, 255)) return sfa.array_alpha(sf)
def fadeEdges(sf, r, startFade=0.9): alphas = sfa.array_alpha(circleFadedEdges(r, startFade)) a = sfa.pixels_alpha(sf) a[:] = alphas del a