Skip to content

Instantly share code, notes, and snippets.

@pmneila
Last active December 16, 2015 05:19
Show Gist options
  • Save pmneila/5383968 to your computer and use it in GitHub Desktop.
Save pmneila/5383968 to your computer and use it in GitHub Desktop.
import sys
import numpy as np
from scipy import ndimage
from scipy.misc import imread, imsave
def transform(X, Y, scale1, scale2):
shape = np.float_(X.max()), np.float_(Y.max())
X = X - shape[0]/2
Y = Y - shape[1]/2
points = np.arctan( (X + 1j*Y) / scale1)*scale2
return np.real(points), np.imag(points)
def map_coordinates_color(img, coords, **kwargs):
num_channels = img.shape[-1]
shape = coords[0].shape
res = np.zeros(shape+(num_channels,), img.dtype)
for i in xrange(num_channels):
res[...,i] = ndimage.map_coordinates(img[...,i], coords, **kwargs)
return res
def animate(img, prefix="img_", num_frames=40, shape=(512,800), scale1=256, scale2=512, autofit=True):
X, Y = np.mgrid[:shape[0], :shape[1]]
X2, Y2 = transform(X, Y, scale1, scale2)
if autofit:
diff = X2.max() - X2.min()
scale3 = np.round(diff/img.shape[0])*(img.shape[0]-1)/diff
X2, Y2 = X2*scale3, Y2*scale3
step = img.shape[0]/float(num_frames)
ratio = img.shape[1]/float(img.shape[0])
for i in xrange(num_frames):
print >> sys.stderr, "Frame %d..."%i
offset_x = i * step
offset_y = offset_x*ratio
aux = map_coordinates_color(img, [X2+offset_x,Y2-offset_y], mode='wrap')
imsave("%s%03d.png"%(prefix, i), aux)
def main():
filename = sys.argv[1]
img = imread(filename)
animate(img)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment