Gorillaz Code

From Predictive Chemistry
Revision as of 12:19, 29 February 2016 by David M. Rogers (talk | contribs)

Jump to: navigation, search

Use the following image files:

L.png R.png Banana.png

And base your work on the following code:

<source lang="python"> import matplotlib.pyplot as plt import Image, time import numpy as np

  1. 0. Import some images

L = np.array(Image.open("L.png")) # 208 x 208 R = np.array(Image.open("R.png")) # 205 x 205 ban = np.array(Image.open("banana.png")) # 105 x 100

L = L[:,:,0] R = R[:,:,0] ban = ban[:,:,0]

  1. print L.shape, R.shape, ban.shape
  2. exit()

x0 = 100 y0 = 50 x1 = 500 y1 = 400

def draw(x, y):

  # 1. create an array
  Z = np.zeros((1000,1000))
  # 2. Add images to array
  Z[x0:x0+208,y0:y0+208] = L
  Z[x1:x1+205,y1:y1+205] = R
  Z[x:x+105,y:y+100] = ban
  return Z

Z = draw(300,50)

  1. 3. show the array
  2. plt.imshow(Z, cmap = plt.cm.gray)
  3. plt.show()
  1. Animate the array by calling draw() 200 times.

def animate(cb, args=(), n=200):

   tstart = time.time()
   data = cb(*args)
   im = plt.imshow(data, origin='lowerleft')
   for i in range(1, n):
           data = cb(*args)
           im.set_data(data)
           fig.canvas.draw()
   print "FPS:", n/(time.time() - tstart)

fig = plt.figure() ax = fig.add_subplot(111) win = fig.canvas.manager.window win.after(100, lambda: animate(draw, (50,100))) plt.show()

</source>