代码之家  ›  专栏  ›  技术社区  ›  vikingosegundo

如何迭代图像的列?

  •  1
  • vikingosegundo  · 技术社区  · 16 年前

    我想像这样变换图像,为django中的图片添加效果 described here

    koala waved

    我决定把它作为一个伟大的过程来实施 django -影像套件/摄影记录

    欢迎提供任何提示(代码、LIN、一般想法)

    1 回复  |  直到 9 年前
        1
  •  6
  •   Denis Otkidach    16 年前

    下面是一个quick-n-dirty示例,它应该能让您找到正确的方向:

    from PIL import Image, ImageOps
    import math
    
    src = Image.open('arched.jpg')
    
    ampl = step = 10
    
    img = ImageOps.expand(src, border=ampl*4, fill='white')
    size = img.size
    
    straight_mesh = {}
    distorted_mesh = {}
    for y in range(size[1]//step-1):
        py = step*(y+1)
        dx = -int(round(ampl*math.sin(py*math.pi/size[1])))
        print dx 
        for x in range(size[0]//step-1):
            px = step*(x+1)
            straight_mesh[x, y] = (px, py)
            distorted_mesh[x, y] = (px+dx, py)
    transform = []
    for x in range(size[0]//step-2):
        for y in range(size[1]//step-2):
            transform.append((
                map(int, straight_mesh[x, y] + straight_mesh[x+1, y+1]),
                map(int, distorted_mesh[x, y] + distorted_mesh[x, y+1] + \
                        distorted_mesh[x+1, y+1] + distorted_mesh[x+1, y])
            ))
    img = img.transform(size, Image.MESH, transform, Image.BICUBIC)
    img = ImageOps.crop(img, border=ampl*2)
    img.save('result.jpg')