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

[pygame] pygame.pixelcopy.array_to_surface(Dest, Src) not working



Hello all,

I think this was somewhere discussed, but I cannot find and this
does not work for me.
So I want to use this function to copy numpy array values to a
surface. I think I do everything right, but it does not work.
pygame version: 1.9.2a0, Windows 7, python 2.7.12

So I use this function, which also checks the shape of input and surface:

def copy_arr(Dest, Src):
    surf_w = Dest.get_width()
    surf_h = Dest.get_height()
    surf_bs = Dest.get_bitsize()
    arr_h = Src.shape[0]
    arr_w = Src.shape[1]
    arr_bs = Src.dtype
    print "surface shape: {} x {}".format(surf_w, surf_h)
    print "surface format: {} ".format(surf_bs)
    #get_bytesize
    print "array shape: {} x {}".format(arr_w, arr_h)
    print "array type: {} ".format(arr_bs)
    pygame.pixelcopy.array_to_surface(Dest, Src)

It gives me:


surface shape: 300 x 200
surface format: 8
array shape: 300 x 200
array type: uint8
Traceback (most recent call last):
  File "fmap.py", line 69, in <module>
    copy_arr(I_surf, I)
  File "fmap.py", line 30, in copy_arr
    pygame.pixelcopy.array_to_surface(Dest, Src)
ValueError: array must match surface dimensions

I create them like:

W = 300
H = 200

I = numpy.zeros((H, W),dtype = "uint8")
I_surf = pygame.Surface((W, H), 0, 8)

So, seems the problem is not in me. Same error is for surfarray.blit_array (), but
I think it depends on pixelcopy.

Aslo this function works fine, which I use now:

def copy_arr(Dest, Src):                   
    buf = Dest.get_buffer()
    buf.write(Src.tostring(), 0)

I just thought there must be more effective way to do it.
Hope somebody will clear that out.

Mikhail