我想像这样转换图像以在 django 中为图片添加效果 此处描述.

koala waved

我决定将其作为一个伟大的过程来实施 姜戈-imagekit/照片记录

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

如何在 PIL 中通过正弦偏移来设计一列像素?

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

有帮助吗?

解决方案

下面是一种快速正脏例子应使你在正确的方向:

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')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top