如何迭代图像的列?

时间:2009-10-12 16:02:38

标签: django python-imaging-library imagekit django-imagekit photologue

我想要像这样转换图像,将效果添加到django中的图片described here

koala waved

我决定将它作为一个伟大的django - imagekit / photologue

的过程来实现

我对PIL的了解不是很好,所以我的问题是

如何通过PIL中的正弦偏移来打算像素列?

欢迎任何提示(代码,lins,一般想法)

1 个答案:

答案 0 :(得分:6)

这是一个快速肮脏的例子,它可以带你走向正确的方向:

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')
相关问题