2

In pygame I'd like to interleave two surfaces before displaying them on screen so that:

  1. Odd numbered lines contain pixels from surface A,
  2. Even numbered lines contain pixels from surface B.

In pygame docs they say this is more efficient to do with pixelArray, which is Surface represented as numpy array. I did this by copying data into a new array, but the result is still very slow.

Is there a more efficient way to do this (I suppose using some numpy method) so pixel data is accessed in original matrices, without copying every pixel?

Saket Mittal
  • 3,726
  • 3
  • 29
  • 49
user1431676
  • 63
  • 1
  • 8
  • How about this? http://stackoverflow.com/a/12861436/2529026 – J David Smith Aug 16 '15 at 21:55
  • David's reference shows some fast ways of doing the copy, but it is still copying. – hpaulj Aug 16 '15 at 23:25
  • 5
    The basic answer is no; the data of the new array must be a copy of the source arrays. From the related sidebar: http://stackoverflow.com/questions/30005735/list-of-numpy-arrays-to-single-numpy-array-without-copying-data and http://stackoverflow.com/questions/29773918/is-there-any-way-to-make-a-soft-reference-or-pointer-like-objects-using-numpy-ar – hpaulj Aug 16 '15 at 23:29
  • Although it's not possible to interleave two pre-existing arrays without copying, what you could do is allocate your output array, then fill alternating row slices with the pixel data for A and B. – ali_m Aug 17 '15 at 22:06

0 Answers0