Ejemplo n.º 1
0
    def filter(self, img):
        '''Effect Kernel of radius based Effect. 
        @param formula is a function like f(x, y) => (x', y'), -1 <= x <= 1 and -1 <= y <= 1
        '''

        try:
            return core.lens_warp(img, self.formula, self.antialias, self.empty_color) 
        except:
            pass
        
        width, height = img.size
        nx, ny = width, height
        new_img = img.copy()
        nband = len(img.getpixel((0, 0)))
        for j in xrange(height):
            for i in xrange(width):
                found = 0
                psum = (0, ) * nband
                new_img.putpixel((i, j), Effect.empty_color)
                # antialias
                for ai in xrange(self.antialias):
                    x = 2 * (i + ai / float(self.antialias)) / width - 1
                    for aj in xrange(self.antialias):
                        y = 2 * (j + aj / float(self.antialias)) / height - 1

                        # distortion 
                        xnew, ynew = self.formula(x, y) 

                        i2 = int(round(0.5 * nx * (xnew + 1)))
                        j2 = int(round(0.5 * ny * (ynew + 1)))

                        if not (0 <= i2 < nx and 0 <= j2 < ny):
                            continue

                        pt = img.getpixel((i2, j2))
                        psum = map(operator.add, psum, pt)
                        found += 1 

                if found > 0:
                    psum = map(operator.div, psum, (found, ) * len(psum)) 
                    new_img.putpixel((i, j), tuple(psum))

        return new_img
Ejemplo n.º 2
0
    def filter(self, img):
        try:
            return core.radian_warp(img, self.formula, self.antialias, self.empty_color)
        except:
            pass

        def radian_formula(x, y):
            '''transform formula
            func is a function that like f(r, phi) => (r, phi)
            '''
            r = sqrt(x ** 2 + y ** 2)
            phi = atan2(y, x)

            r, phi = self.formula(r, phi)

            xnew = r * cos(phi)
            ynew = r * sin(phi)

            return xnew, ynew

        warp = LensWarpEffect(radian_formula, self.antialias)
        return warp(img)