[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Exception in _numpysurfarray.py



Hi all,

a friend of mine had a problem using pygame.surarray.pixels3d
creation would raise the following Exception:

File "C:\Python25\lib\site-packages\pygame\surfarray.py", line 165, in pixels3d
    return numpysf.pixels3d (surface)
File "C:\Python25\lib\site-packages\pygame\_numpysurfarray.py", line 213, in pixels3d
    array.shape  = surface.get_width (), surface.get_height (), bpp
AttributeError: incompatible shape for a non-contiguous array

here is the code section in question from the _numpysurfarray.py:

    array = numpy.frombuffer (surface.get_buffer (), numpy.uint8)
    array.shape = surface.get_height (), surface.get_pitch ()
    # I believe this line make the array in some cases non-contiguous
    array = array[:,:surface.get_width () * bpp]
    # the next line raises the exception
    array.shape  = surface.get_width (), surface.get_height (), bpp
    array = array[:,:,start:end:step]
    return array

My analysis was that the line before makes the array non-contiguous and thus the direct assignment of the shape attribute fails. reshape copies the data to a new array if necessary. I might be wrong since I never had this problem myself (it might be due to the fact that I'm on Linux while my friend is using MS Windows).

I attached a patch that uses reshape instead of the direct assignment.
I left all the other places where array.shape is assigned untouched because they are mostly directly after the arrays creation and should therefor always be contiguous.

Please can someone (with more numpy experience than myself) look at this?

yours
//Lorenz
--- _numpysurfarray.py	2008-04-06 13:47:47.000000000 +0200
+++ _numpysurfarray_patched.py	2008-04-09 20:37:00.000000000 +0200
@@ -210,7 +210,7 @@
     array = numpy.frombuffer (surface.get_buffer (), numpy.uint8)
     array.shape = surface.get_height (), surface.get_pitch ()
     array = array[:,:surface.get_width () * bpp]
-    array.shape  = surface.get_width (), surface.get_height (), bpp
+    array = numpy.reshape(array, (surface.get_width (), surface.get_height (), bpp))
     array = array[:,:,start:end:step]
     return array