From 95d2c8df83734d5d5990c3d4e09b898405a3772c Mon Sep 17 00:00:00 2001 From: Cedric Porter Date: Thu, 26 Jul 2012 21:24:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=8F=E8=A7=86=E5=8F=98=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EffectLab/Effect.py | 105 ++++++++++++++++++++++++++++---------------- TestEffectLab.py | 6 +-- 2 files changed, 70 insertions(+), 41 deletions(-) diff --git a/EffectLab/Effect.py b/EffectLab/Effect.py index b73ac73..ad5370b 100755 --- a/EffectLab/Effect.py +++ b/EffectLab/Effect.py @@ -34,6 +34,35 @@ def __call__(self, img): def filter(self, img): return img +class PerspectiveWarpEffect(Effect): + '''透视变换 + ''' + def __init__(self, + lefttopoffset=(0, 0), + righttopoffset=(0, 0), + rightbottomoffset=(0, 0), + leftbottomoffset=(0, 0), + ): + self.offsets = [lefttopoffset, + righttopoffset, + rightbottomoffset, + leftbottomoffset, + ] + + def filter(self, im): + import cv, numpy + + width, height = im.size + orig = [(0,0),(width - 1,0),(width - 1, height - 1),(0, height - 1)] + pos = map(lambda lhs, rhs: (lhs[0] + rhs[0], lhs[1] + rhs[1]), orig, self.offsets) + + mat = cv.CreateMat(3, 3, cv.CV_32FC1) + cv.GetPerspectiveTransform(pos, orig, mat) + a = numpy.asarray(mat) + + im = im.transform(im.size, Image.PERSPECTIVE, a.flatten(), Image.BILINEAR) + return im + class RegionWarpEffect(Effect): def __init__(self, formula, antialias=2, box=None): @@ -253,44 +282,44 @@ def filter(self, img): warp = RegionWarpEffect(f, self.antialias) return warp(img) -class WaveEffect(Effect): - name = 'Wave Effect' - def __init__(self, vertical_delta=0.1, horizon_delta=0, box=None): - self.vertical_delta = vertical_delta - self.horizon_delta = horizon_delta - self.box = box - - def filter(self, img): - if self.box == None: - left, top = 0, 0 - right, bottom = img.size[0] - 1, img.size[1] - 1 - else: - left, top, right, bottom = self.box - - width, height = img.size - - mid_x = (right + left) / 2.0 - mid_y = (top + bottom) / 2.0 - - new_img = img.copy() - height_delta = (bottom - top + 1) * self.vertical_delta - width_delta = 2 * math.pi / (right - left + 1) * (self.horizon_delta + 1) - for x in xrange(left, right): - degree = x * width_delta - for y in xrange(top, bottom): - - # distortion - h = sin(degree) * height_delta * ((bottom - top) / 2 - sqrt((y - mid_y) ** 2 + (x - mid_x) ** 2)) / mid_y - offset = int(round(h)) - _x = x - _y = y + offset - - if 0 < x < width and 0 < _y < height: - new_img.putpixel((x, y), img.getpixel((x, _y))) - else: - new_img.putpixel((x, y), Effect.empty_color) - - return new_img +# class WaveEffect(Effect): +# name = 'Wave Effect' +# def __init__(self, vertical_delta=0.1, horizon_delta=0, box=None): +# self.vertical_delta = vertical_delta +# self.horizon_delta = horizon_delta +# self.box = box + +# def filter(self, img): +# if self.box == None: +# left, top = 0, 0 +# right, bottom = img.size[0] - 1, img.size[1] - 1 +# else: +# left, top, right, bottom = self.box + +# width, height = img.size + +# mid_x = (right + left) / 2.0 +# mid_y = (top + bottom) / 2.0 + +# new_img = img.copy() +# height_delta = (bottom - top + 1) * self.vertical_delta +# width_delta = 2 * math.pi / (right - left + 1) * (self.horizon_delta + 1) +# for x in xrange(left, right): +# degree = x * width_delta +# for y in xrange(top, bottom): + +# # distortion +# h = sin(degree) * height_delta * ((bottom - top) / 2 - sqrt((y - mid_y) ** 2 + (x - mid_x) ** 2)) / mid_y +# offset = int(round(h)) +# _x = x +# _y = y + offset + +# if 0 < x < width and 0 < _y < height: +# new_img.putpixel((x, y), img.getpixel((x, _y))) +# else: +# new_img.putpixel((x, y), Effect.empty_color) + +# return new_img class EffectGlue(Effect): diff --git a/TestEffectLab.py b/TestEffectLab.py index 4fe2c6b..6cdadb4 100755 --- a/TestEffectLab.py +++ b/TestEffectLab.py @@ -39,13 +39,13 @@ def main(): print 'Started' effects = [ - RadianFormulaEffect(lambda r, phi: (r ** 2, phi), 4), + LocalWarpEffect((50, 20), (80, 40), 30), RadianSqrtEffect(), - GlobalWaveEffect(), LensWarpEffect(lambda x, y: (sign(x) * x ** 2, sign(y) * y ** 2)), + RadianFormulaEffect(lambda r, phi: (r ** 2, phi), 4), + GlobalWaveEffect(), LensWarpEffect(lambda x, y: (sin(x * math.pi / 2), sin(y * math.pi / 2))), RadianFormulaEffect(lambda r, phi: (r ** 1.5 * math.cos(r), phi)), - LocalWarpEffect((130, 120), (130, 50), 100), ] # if os.path.exists('z.jpg'):