Skip to content

Commit

Permalink
透视变换
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricporter committed Jul 26, 2012
1 parent 49da060 commit 95d2c8d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 41 deletions.
105 changes: 67 additions & 38 deletions EffectLab/Effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions TestEffectLab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down

0 comments on commit 95d2c8d

Please sign in to comment.