Ripples

Playing with image mappings

Gallery

Metadata

I wanted a looping animation that appears to deform space continuously. This restricts the mapping to be continuous and bijective. Arbituarily, I choose the domain to be the unit disc and the mapping to be $z = r \exp{(ix)} \rightarrow \text{frac}(r+c) \exp{(ix)}$. Perhaps other mappings might be interesting.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from PIL import Image, ImageDraw, ImageFont, ImageChops
import numpy as np

def trim(im):
    i = np.array(im)
    i[i[:,:,3] == 0] = 0
    while True:
        if i[0].max() == 0: i = i[1:,:,:]
        else: break
    while True:
        if i[-1].max() == 0: i = i[:-1,:,:]
        else: break
    while True:
        if i[:,0].max() == 0: i = i[:,1:,:]
        else: break
    while True:
        if i[:,-1].max() == 0: i = i[:,:-1,:]
        else: break
    return Image.fromarray(i)

def str2img(s:str, fz:int):
    
    img = Image.new("RGBA", (300*len(s),300), (255,255,255,0))
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype(font_path, fz)

    draw.text((10, 0), s, (0,0,0), font=font)
    return trim(img)

def to_img(arr):
    return Image.fromarray((np.clip(arr, 0, 1).astype(np.float)*255).astype(np.uint8))

def to_arr(img):
    return np.array(img).astype(np.float) / 255

def place_text(canvas, s, fz, col):
    
    t = to_arr(str2img(s,fz))
    t[t[:,:,-1] > 0] = col
    x,y,_ = t.shape
    cx,cy,_ = canvas.shape
    assert cx >= x and cy >= y
    dx = int(.5*(cx-x)); dy = int(.5*(cy-y))
    canvas[dx:dx+x,dy:dy+y,:] += t
    return canvas

K = 2
def transform(canvas, c, _cache={}):
    _cache = {}
    cx,cy,_ = canvas.shape
    nc = np.zeros((cx,cy,4))

    def finv(x,y):
        x,y = x/cx * K - K/2, y/cy * K - K/2
        z = complex(x,y)
        
        r = abs(z)
        if r == 0: return True, 0,0
        r = (r+c)%1
        if r < 0: return False, 0,0
        z = z/abs(z) * r
        
        x,y = z.real, z.imag
        x,y = (x+K/2)/K * cx, (y+K/2)/K * cy
        return 0<=x<cx-1 and 0<=y<cy-1, x,y
    
    if (cx,cy,c) in _cache:
        finv_arr = _cache[(cx,cy,c)]
    else:
        finv_arr = [
            [finv(x,y) for x in range(cx)]
            for y in range(cy)
        ]
        _cache[(cx,cy,c)] = finv_arr
    
    for x in range(cx):
        for y in range(cy):
            
            if not (0<x<cx-1 and 0<y<cy-1): continue
            
            sxy = [finv_arr[y+j][x+k] for j in [0] for k in [0]]
            if sum(s for s,_,_ in sxy) != len(sxy): continue
            
            for _,nx,ny in sxy:
                iy,ix = int(ny), int(nx)
                fy,fx = ny%1, nx%1
                nc[x,y] += (
                    canvas[ix,iy] * fy * fx + 
                    canvas[ix+1,iy] * (1-fx) * fy +
                    canvas[ix,iy+1] * (1-fy) * fx +
                    canvas[ix+1,iy+1] * (1-fy) * (1-fx)
                )
            nc[x,y] /= len(sxy)
            
    return nc

J = 1
canvas = np.zeros((1800//J,1800//J,4))
canvas += np.array([7, 42, 95, 255], dtype=np.float)/255

canvas = place_text(canvas, "hello", 260//J, np.array([0, 1, 0.7, 1.]))
canvas = transform(canvas, 0.5)
canvas = place_text(canvas, "world", 260//J, np.array([1, 0.66, 0, 1.]))
canvas = transform(canvas, 0.5)

nc = to_img(canvas)
nc = to_arr(nc)

nframes = 80
cs = np.linspace(0,1,nframes)

for idx,c in list(enumerate(cs)):
    print(idx, end="\r")
    _nc = transform(nc, c)
    _nc = to_img(_nc)
    _nc.save(f"out/{idx}.png")

def gen_frame(path):
    im = Image.open(path)
    alpha = im.getchannel('A')
    im = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)
    mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0)
    im.paste(255, mask)
    im.info['transparency'] = 255
    return im

imgs = [gen_frame(f"out/{idx}.png") for idx in range(nframes-1)][::-1]
imgs[0].save('dist/out.gif', save_all=True, append_images=imgs[1:])