Motivation
The Transform class is intended to be used to precalculate pixel-based "special effects" on an Image. Aside from the predefined transformations, the user can specify a callback that will be used to modify each pixel in the image.
Usage
Creation
The constructor of the Transform class has two forms.
tr = psp2d.Transform(operation, param)
The first one builds a Transform object performing one of the predefined transformations. 'operation' is an integer which may be one of the following:
psp2d.TR_PLUS psp2d.TR_MULT
'param' is a floating-point parameter. A TR_PLUS transform will add 'param' to each red, green, and blue (not alpha) component of the image's pixels. TR_MULT will multiply the components by 'param'. This is quite fast since all the computing is done at the C++ level.
tr = psp2d.Transform(cb)
This second form is more flexible, but slower. 'cb' must be a Python callable object (for instance, a function). For each pixel in the image, this callback will be called with parameters (x, y, color). (x, y) is the position of the pixel and color is a Color object that may be changed in place. Its final value will be affected to the pixel.
The callback must return True, or else the transformation stops at this pixel.
Applying
Once a Transform object is created, it may be applied to an image through its apply method.
tr.apply(img)
Examples
To transform a color image, 'img', in its grayscale version:
def grayit(x, y, color): value = (color.red + color.green + color.blue) / 3 color.red = value color.green = value color.blue = value return True tr = psp2d.Transform(grayit) tr.apply(img)
This one will only keep a circular region of the image, fading when the pixel is far from the center:
import math class Circle: def __init__(self, w, h): self.w = w self.h = h def __call__(self, x, y, color): dx = 2.0 * (x - self.w / 2) / self.w dy = 2.0 * (y - self.h / 2) / self.h r = math.sqrt(dx*dx + dy*dy) / math.sqrt(2) color.alpha = int(255 * (1.0 - r)) return True tr = psp2d.Transform(Circle(img.width, img.height)) tr.apply(img)
Notes
The version that takes a user callback may prove very slow, because for each pixel in the image, a Python Color object is created and a Python method call is done. Use this only to precalculate effects, not in real time.
