1

我想變換像這樣的圖像,以便爲django中的圖片添加效果,如described here如何迭代圖像的列?

koala waved

我決定實現它作爲偉大django -imagekit/photologue過程

我PIL的知識不是很好,所以我的問題是

如何在PIL中用一個正弦偏移量來計算一列像素?

任何提示(代碼,林家,一般的想法),歡迎

回答

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')