grayscale pixel based graphics with pygame

Kamilche klachemin at comcast.net
Wed Mar 8 23:53:04 EST 2006


import pygame

def main():
    SIZE = (300, 200)
    WHITE = (255, 255, 255)
    pygame.init()

    # Create a new grayscale surface
    pic = pygame.Surface(SIZE, 0, 8)
    palette = tuple([(i, i, i) for i in range(256)])
    pic.set_palette(palette)

    # Fill it with a gradient
    array = pygame.surfarray.pixels2d(pic)
    factor = 256.0/SIZE[1]
    for i in range(SIZE[1]):
        array[:, i] = int(factor * i)

    # Draw a star on it
    data = (144, 18), (199,166), (63,73), (221, 71), (79,159)
    pygame.draw.lines(pic, WHITE, 1, data)

    # Save the image and quit
    pygame.image.save(pic, 'temp.bmp')
    pygame.quit()

main()




More information about the Python-list mailing list