title: Using OpenCV-Python, how to use Python lists and Numpy arrays together efficiently?
body: <p>I'm trying to recreate a live video slitscan effect that I saw at an art museum.</p>
  
  <p>Using opencv-python the program keeps a buffer of the last 480 frames from a webcam and then combines one row of pixels from each frame into a single image. I wrote code that produces the desired effect, but it runs a little slower than I would like. How can I optimize the algorithm by using python lists and numpy arrays together in the most efficient way?</p>
  
  <p>Be aware it takes 10 seconds or so to fill the buffer before you will see the video effect.</p>
  
  <pre><code>import numpy as np
  import cv2
  
  cap = cv2.VideoCapture(0)
  
  # originally tried to use numpy to store the buffer
  #buffer = np.zeros(shape=(480,480,640,3),dtype=np.uint8) 
  buffer = []
  slices = []
  f2 = np.zeros(shape=(480,640,3),dtype=np.uint8)
  
  display = False
  rows = np.arange(480)
  
  while(True):
      # Capture frame-by-frame
      ret, frame = cap.read()
      frame = cv2.flip(frame, -1) # flipping the frame to get the scan lines to move downward
  
      # originally I tried to make the buffer like this but get a memory error    
      #buffer = np.roll(buffer, -1, axis=0)
      #buffer[-1] = frame
  
      # using a list of numpy arrays seems to be more efficient for storing the buffer   
      buffer.append(frame)
      if(len(buffer) &gt; 480):
          buffer = buffer[1:]
  
          # using a list to combine a row of pixels from each frame
          # could this be optimized with numpy arrays?
          for i in range(len(buffer)):
              slices.append(buffer[i][i])
  
          # convert each row to a numpy array to store final frame for display
          f2[rows] = np.array(slices[::-1])
          slices.clear()
          cv2.imshow('frame', f2)
  
      if cv2.waitKey(25) &amp; 0xFF == ord('q'):
          break
  
  # When everything done, release the capture
  cap.release()
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>The other idea I had would be to rewrite the code to use less buffer frames and combine several rows from each frame together, but I want to try to optimize the code with the full resolution video if possible.</p>