Title: Vertical Image Wipe EffectAuthor: Gareth 'Korruptor' Noyce (g at korruptor.demon.co.uk) Description: A function that will progressively 'wipe' a supplied image with a given color, using surfarray. Download: vertical_alpha-wipe.zip pygame version required: Any (with surfarray) Comments: Mr. Noyce is clearly enjoying playing with Numeric and Surfarray; I've gotten a flurry of effects submissions from him in the past month. This function gradually 'wipes' an image from top to bottom with a solid color; alpha effects keep the transition gradual and attractive. The code invites generalization - as Mr. Noyce mentions in the included readme file, horizontal and diagonal wipes should be easy to add. A sample image is included, so you can see the function in action when the module is run as a script. |
import pygame, pygame.image from pygame.surfarray import * from pygame.locals import * # ------------------------------------------------------------------------------------ # Glob decs # Screen resolution... RES = (320,200) # ------------------------------------------------------------------------------------ def main(): # Initialise pygame, and grab an 8bit display. pygame.init() screen_surface = pygame.display.set_mode(RES, FULLSCREEN) # Load a dummy image image = pygame.image.load("pygame_small.bmp") # Give it some colourkey info... image.set_colorkey((255,255,255)) # Create a surface suitable for pixels_alpha() image_surface = pygame.Surface((image.get_size()), SRCALPHA, 32) # Blit the dummy image onto it... image_surface.blit(image, (0,0)) # Call our "fade" module, and wait for it to do it's stuff... do_vert_alpha_fade(image_surface, 0, 16, (255,255,0), screen_surface) # Bye! return # ------------------------------------------------------------------------------------ def do_vert_alpha_fade(image_surface, offset, num_fade_rows, background_colour, display_surface): """ For a given number of rows, decrements the alpha of all pixels on the row. By contantly moving down a row, we cn fade out the entire surface...""" size = image_surface.get_size() # Calculate the amount we will decerement each alpha value per row... # It helps if num_rows is a whole divisor of 256... fade_factor = int(256/num_fade_rows) # for each row in the image_surface... for i in range(0,(size[1]+num_fade_rows)): # Make a new array of alpha values for the surface as we deleted the last one... arr = pygame.surfarray.pixels_alpha(image_surface) # Increment the scanline offset to move to the next row... offset +=1 for i in range(0,num_fade_rows): # Bounds check... if((offset - i) < 0): # Ignore rows "above" screen... break if((offset - i) > size[1] - 1): # Ignore rows "below" screen, but continue as we need to process all the rows in the surface $row times... continue else: # Images with fixed alpha values will allow you to optimise this loop away: # Essentially you can insert one big row of new (identical) alpha values at a time # instead of iterating over each pixel, decrementing the individual alpha value for each... # # Unforuntately, I'm fairly sure I'll need vairable alpha ranges for my logos, so per pixel # accuracy is going to be useful. Hence the slow version! :-) # for each pixel in this row... for j in range(0, (size[0]-1)): # Check it's current value... if (arr[j][offset-i] <= fade_factor): # Hmmm, number smaller than what we plan to decrement it by. # Set it to zero (to prevent 8bit-int looping and giving us a full alpha value again!) arr[j][offset-i]=0 else: # Or decrement it by the fade amount... arr[j][offset-i] -= fade_factor # Right, delete the alpha_array so the surface is no longer locked... del arr # Display the newly calculated surface over the supplied background colour... display_surface.fill(background_colour) display_surface.blit(image_surface, (0, 64)) # Show our audience... pygame.display.update() # ------------------------------------------------------------------------------------ if __name__ == '__main__': main() # End of sauce. Pass the chips...
Main - Repository - Submit - News |