djangopython-imaging-libraryimagekitdjango-imagekitphotologue

How to iterate over columns of an image?


I want to transform images like this to add an effect to pictures in django as described here.

koala waved

I decided to implement it as a process for the great django-imagekit/photologue

My knowledge of PIL isn't very good, so my question is

How to intend a column of pixels by a sinus-offset in PIL?

any hints (code, lins, general thoughts) are welcomed


Solution

  • Here is an quick-n-dirty example which should bring you to the right direction:

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