Title: Mosaic Transition

Author: Gareth Noyce (korruptor at mac.com)
Submission date: June 29, 2002

Description: A fading, pixellizing mosaic transition effect.

Download: mosaic-transition.zip

pygame version required: 1.3.4
SDL version required: Any
Python version required: Python 2 (with Numeric)

Comments: Here's another nice transition effect from Gareth Noyce. This one pixellates and fades a given image, and then restores it. You'll need surfarray, and a reasonably current version of pygame. The supplied script demonstrates the effect on a nice sample image, by fading and restoring the image in a loop. Just hit a key or a mouse button to quit.

Messages: 0

Program		:	mosaic.py
Author		:	Gareth "Korruptor" Noyce, http://www.korruptor.demon.co.uk
Description	:

This is a stupidly simple mosaic transition for a surface, essentially the same as the
transition Nintendo used in Super Mario World Advance 2 (Super Mario World on the SNES).

I've been stuck in traffic for 45mins, and I've been so bored that this is the result. :)

The mosaic function takes a destination surface, a source surfarray and a value for the size 
of each "pixel" it creates in the destination. The result is a "chunky" version of the source.

This version works on an 8bit palette, but it's trivial to convert to true colour... You may 
also want to precalc the first two frames to avoid the "stutter" that happens on largish


import pygame, pygame.image
from pygame.surfarray import *
from pygame.locals import *
from Numeric import *

# ------------------------------------------------------------------------------------

RES 		= (400,300)
PALETTE 	= zeros((256,3))
FADE_STEP	= zeros((256,3))
FRAMES          = 16

# ------------------------------------------------------------------------------------
def main():
    	# Initialise pygame, and grab an 8bit display.
    	screen		= pygame.display.set_mode(RES,0, 8)

	# Load a dummy image and copy it to a surfarray
    	image 		= pygame.image.load("pygame.gif")
        image_buf 	= pygame.surfarray.array2d(image)  
        # This will be the initial size of the mosaic pixels...
        pixel_chunk = 2
        dir = 1

        # Fruity loops...
        while 1:
            # Have we received an event to quit the program?
            for e in pygame.event.get():
                if e.type in (QUIT,KEYDOWN,MOUSEBUTTONDOWN):

            # Get the image palette
            PALETTE = image.get_palette()

            # Blit the original image...

            # Calculate the amounts to inc/dec each colour per frame
            # so we can fade to black. You could fade each colour by
            # a set amount, but this will ensure all colours hit black 
            # at the same time (looks a bit better imo)
            for i in range(0,256):
                FADE_STEP[i][0] = int(PALETTE[i][0] / FRAMES) 
                FADE_STEP[i][1] = int(PALETTE[i][1] / FRAMES)
                FADE_STEP[i][2] = int(PALETTE[i][2] / FRAMES)

            # Fade out
            for i in range(0,FRAMES):
                pixelate(screen, image_buf, pixel_chunk)
                PALETTE -= FADE_STEP
                pixel_chunk +=2

            # Fade in
            for i in range(0,FRAMES):
                pixelate(screen, image_buf, pixel_chunk)
                PALETTE += FADE_STEP
                pixel_chunk -=2

        # Bye!

# ------------------------------------------------------------------------------------
def pixelate(dest, src, size):
    Generates a mosaic of the src image to the dest surface. Size dictates the pixel size

    - Note: you should really average out the colour values of all pixels from 
      x,y to x+size,y+size for accuracy. This is just a quick hack, and as I'm fading the 

    for y in range(0,RES[1],size):
        for x in range(0,RES[0],size):
            rtile 	= pygame.Rect([x,y,size,size])
            colour 	= src[x][y]
            dest.fill(colour, rtile)

# ------------------------------------------------------------------------------------
if __name__ == '__main__': main()
# End of sauce. Pass the chips...

Main - Repository - Submit - News