# HG changeset patch # User Jeremy Thurgood # Date 1328974895 -7200 # Node ID 1eb1537173ef960e029814b522f4ba95b9f062e3 # Parent 4e9178215e7509d81b5ab116a464cb5ed6177580 Add some image transforms. diff -r 4e9178215e75 -r 1eb1537173ef pyntnclick/image_transforms.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyntnclick/image_transforms.py Sat Feb 11 17:41:35 2012 +0200 @@ -0,0 +1,63 @@ +"""Transforms to apply to images when they're loaded.""" + +from pygame.transform import rotate +from pygame.locals import BLEND_RGBA_MULT, SRCALPHA +from pygame.surface import Surface + + +class Transform(object): + + def __init__(self, func, *args): + self._func = func + self._args = args + + def __call__(self, image): + return self._func(image, *self._args) + + def __hash__(self): + return hash((id(self._func), self._args)) + + def __eq__(self, other): + return (self._func is other._func) and self._args == other._args + + def __repr__(self): + return "<%s args=%r>" % (self.__class__.__name__, self._args) + + +# transform that does nothing +NULL = Transform(lambda x: x) + +# base rotation transforms +R90 = Transform(rotate, 90) +R180 = Transform(rotate, 180) +R270 = Transform(rotate, -90) + + +# overlays +class Overlay(Transform): + """Overlay another image on top of the given one.""" + + def __init__(self, resources, image_name_fragments, blend=0): + super(Overlay, self).__init__( + self.overlay, resources, image_name_fragments, blend) + + def overlay(self, image, resources, image_name_fragments, blend): + image = image.copy() + overlay = resources.load_image(image_name_fragments) + image.blit(overlay, (0, 0), None, blend) + return image + + +# colour overlays +class Colour(Transform): + """Overlay an image with a colour.""" + + def __init__(self, colour, blend=BLEND_RGBA_MULT): + super(Colour, self).__init__(self.colour, colour, blend) + + def colour(self, image, colour, blend): + image = image.copy() + overlay = Surface(image.get_size(), SRCALPHA, image) + overlay.fill(colour) + image.blit(overlay, (0, 0), None, blend) + return image diff -r 4e9178215e75 -r 1eb1537173ef pyntnclick/resources.py --- a/pyntnclick/resources.py Sat Feb 11 17:38:57 2012 +0200 +++ b/pyntnclick/resources.py Sat Feb 11 17:41:35 2012 +0200 @@ -66,8 +66,7 @@ image name out of several fragments. The `transforms` param may contain transforms, which modify an image - in-place to apply various effects. TODO: Implement transforms - somewhere, so this becomes useful. + in-place to apply various effects. The `basedir` param defaults to 'images', but may be overriden to load images from other places. ('icons', for example.) @@ -90,7 +89,7 @@ self._image_cache[image_path] = image image = self._image_cache[image_path] - # Apply any mutators we're given. + # Apply any transforms we're given. for transform in transforms: image = transform(image) self._transformed_image_cache[key] = image