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

Re: [pygame] Exception in _numpysurfarray.py



Lorenz Quack wrote:
Marcus von Appen wrote:
On, Thu Apr 10, 2008, Lenard Lindstrom wrote:

[...]
The problem always comes down to 24 bit per pixel surfaces.

import numpy
numpy.__version__
'1.0.4'
import pygame
surface = pygame.Surface((7,2), 0, 24)
bpp = surface.get_bytesize()
print bpp, surface.get_width()*bpp, surface.get_pitch()
3 21 24
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
Traceback (most recent call last):
 File "<pyshell#18>", line 1, in <module>
   array.shape  = surface.get_width (), surface.get_height (), bpp
AttributeError: incompatible shape for a non-contiguous array

Thanks. Though the numpy.reshape () method might return a copy if
needed, it seems to be safe enough for our cases as the shape changes
are made on the array itself all the time. At least i I can trust
the id() and my eyes ;-).

Thanks for the fix, Lorenz, it's commited in rev. 1199.

Regards
Marcus

The patch may have been a bit premature:
The person who initially reported this problem to me tells me that with this fix applying changes to the pixels3d array does not change the original surface. I guess that numpy.reshape() making a copy does cause problems in some cases. I'm out of ideas how to go about this...

That's peculiar.

>>> import numpy
>>> help(numpy.ndarray.reshape)

Help on method_descriptor:

reshape(...)
   a.reshape(d1, d2, ..., dn, order='c')

Return a new array from this one. The new array must have the same number of elements as self. Also always returns a view or raises a ValueError if
   that is impossible.


Unless I am mistaken a view is an array that references another array.

--
Lenard Lindstrom
<len-l@xxxxxxxxx>