Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created August 18, 2016 12:20
Show Gist options
  • Select an option

  • Save TimSC/078ac63f9b0ba1d3039eea942f2eb93f to your computer and use it in GitHub Desktop.

Select an option

Save TimSC/078ac63f9b0ba1d3039eea942f2eb93f to your computer and use it in GitHub Desktop.
Generated moving checkered flag
import numpy as np
import matplotlib.pyplot as plt
from skimage.transform import PiecewiseAffineTransform, warp, resize
from skimage import data
from PIL import Image, ImageDraw
import numpy as np
import scipy.misc as misc
image = Image.new("RGBA", (512, 512))
draw = ImageDraw.Draw(image)
sqSize = 128
halfSqSize = sqSize/2
draw.rectangle([0,0,image.size[0],image.size[1]], fill=(0,0,0,255))
for x in range(0, 512, sqSize):
for y in range(0, 512, sqSize):
draw.rectangle([x,y,x+halfSqSize,y+halfSqSize], fill=(255,255,255,255))
draw.rectangle([x+halfSqSize,y+halfSqSize,x+sqSize,y+sqSize], fill=(255,255,255,255))
del draw
image = np.array(image)
rows, cols = image.shape[0], image.shape[1]
src_cols = np.linspace(0, cols, 20)
src_rows = np.linspace(0, rows, 10)
src_rows, src_cols = np.meshgrid(src_rows, src_cols)
src = np.dstack([src_cols.flat, src_rows.flat])[0]
# add sinusoidal oscillation to row coordinates
for i in range(10):
print i
step = 2.0 * i / np.pi
dst_rows = src[:, 1] - np.sin(np.linspace(0, 3 * np.pi, src.shape[0])+step) * 30
dst_cols = src[:, 0]
dst_rows *= 1.5
dst_rows -= 1.5 * 50
dst = np.vstack([dst_cols, dst_rows]).T
tform = PiecewiseAffineTransform()
tform.estimate(src, dst)
out_rows = image.shape[0] - 1.5 * 50
out_cols = cols
out = warp(image, tform, output_shape=(out_rows, out_cols))
misc.imsave("flag{0}.png".format(i), out)
#outResize = resize(out, (64, 64))
#misc.imsave("flag{0}.png".format(i), outResize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment