luxkun / unabomber

Bomberman clone

This URL has Read+Write access

unabomber / pil.py
100644 41 lines (36 sloc) 1.327 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import pygame
import Image
 
def _triplets(pil_palette):
    palette = []
    for index in range(0, len(pil_palette), 3):
        rgb = pil_palette[index:index + 3]
        palette.append(rgb)
    return palette
 
def _image_window(pil_image):
    window = (0, 0) + pil_image.size
    if len(pil_image.tile) > 0:
        window = pil_image.tile[0][1]
    return window
    
def _to_pygame_image(pil_image, palette):
    (x0, y0, x1, y1) = _image_window(pil_image)
    image = pygame.image.fromstring(pil_image.tostring(), pil_image.size, 'P')
    image.set_palette(palette)
    image.set_colorkey(pil_image.info['transparency'])
    new_image = pygame.Surface(pil_image.size, pygame.SRCALPHA)
    new_image.blit(image, (x0, y0), (x0, y0, x1 - x0, y1 - y0))
    return new_image
 
def load_gif(image_filename, xscale=1,yscale=1, size=None):
    image = Image.open(image_filename)
    palette = _triplets(image.getpalette())
    images = []
    if not size:
        scaled_size = (int(image.size[0]*xscale), int(image.size[1]*yscale))
    else:
        scaled_size = size
    try:
        while 1:
            images.append(pygame.transform.scale(_to_pygame_image(image, palette), scaled_size))
            image.seek(image.tell() + 1)
    except EOFError:
        pass # end of sequence
    return (image.info['duration'], images)