Mercurial > boomslang
comparison pyntnclick/image_transforms.py @ 593:1eb1537173ef pyntnclick
Add some image transforms.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 11 Feb 2012 17:41:35 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
592:4e9178215e75 | 593:1eb1537173ef |
---|---|
1 """Transforms to apply to images when they're loaded.""" | |
2 | |
3 from pygame.transform import rotate | |
4 from pygame.locals import BLEND_RGBA_MULT, SRCALPHA | |
5 from pygame.surface import Surface | |
6 | |
7 | |
8 class Transform(object): | |
9 | |
10 def __init__(self, func, *args): | |
11 self._func = func | |
12 self._args = args | |
13 | |
14 def __call__(self, image): | |
15 return self._func(image, *self._args) | |
16 | |
17 def __hash__(self): | |
18 return hash((id(self._func), self._args)) | |
19 | |
20 def __eq__(self, other): | |
21 return (self._func is other._func) and self._args == other._args | |
22 | |
23 def __repr__(self): | |
24 return "<%s args=%r>" % (self.__class__.__name__, self._args) | |
25 | |
26 | |
27 # transform that does nothing | |
28 NULL = Transform(lambda x: x) | |
29 | |
30 # base rotation transforms | |
31 R90 = Transform(rotate, 90) | |
32 R180 = Transform(rotate, 180) | |
33 R270 = Transform(rotate, -90) | |
34 | |
35 | |
36 # overlays | |
37 class Overlay(Transform): | |
38 """Overlay another image on top of the given one.""" | |
39 | |
40 def __init__(self, resources, image_name_fragments, blend=0): | |
41 super(Overlay, self).__init__( | |
42 self.overlay, resources, image_name_fragments, blend) | |
43 | |
44 def overlay(self, image, resources, image_name_fragments, blend): | |
45 image = image.copy() | |
46 overlay = resources.load_image(image_name_fragments) | |
47 image.blit(overlay, (0, 0), None, blend) | |
48 return image | |
49 | |
50 | |
51 # colour overlays | |
52 class Colour(Transform): | |
53 """Overlay an image with a colour.""" | |
54 | |
55 def __init__(self, colour, blend=BLEND_RGBA_MULT): | |
56 super(Colour, self).__init__(self.colour, colour, blend) | |
57 | |
58 def colour(self, image, colour, blend): | |
59 image = image.copy() | |
60 overlay = Surface(image.get_size(), SRCALPHA, image) | |
61 overlay.fill(colour) | |
62 image.blit(overlay, (0, 0), None, blend) | |
63 return image |