[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] surfarray woes



Jesse David Andrews wrote:
> but when I was trying to do the "blur" I ran into trouble:
> 
> myarray[1:,:,:]  += myarray[:-1,:,:]*8
> or
> myarray[1:,:] += myarray[:-1,:]*8
> or
> myarray[1:,:] += myarray[1:,:]*1
> 
> all return: return array has incorrect type

here is the problem. the += operator is trying to "add in place" two
arrays. the problem is we are mixing types of arrays and Numeric won't
easily let us put an array of ints into an array of unsigned bytes.

the array we get from array3d is an array of unsigned bytes. the array you
are getting from myarray*8 is an array of ints. since Numeric treats the
"8" as an integer. you could change the 8 to a single element array of
bytes like this, "myarray * array([8], UnsignedInt8)". but then you will
quickly have problems with unsigned bytes running out of precision.

you will likely want to convert the array from array3d into an array of
integers. that would be
    myarray=pygame.surfarray.array3d(img).as_type(Int)
the only drawback to that is Numeric will make an array of bytes, then copy
that into an array of ints. a wasteful second copy of data.

this is one thing i don't like about how Numeric works. it will silently
and easily convert arrays into more capable types, but refuse to
automatically apply things into lower precision types. the only way around
it seems to be manually making your own temporary copies of the array to
the necessary type. for doing image work like in surfarray though, we
specifically want the precision to be lost as we cast to lower types.

hmm, it seems like the array3d function should create an array of integers
anyways. you will rarely want the unsigned bytes it returns, because there
aren't many operations you can do on them.

i have recently done a much faster version of the blur. the code for it
looks like this..

        tmp = pygame.surfarray.array3d(worksurf).as_type(Int)
	workarr = zeros(tmp.shape)
        workarr[1:,:]  = tmp[:-1,:]
        workarr[:-1,:] += tmp[1:,:]
        workarr[:,1:]  += tmp[:,:-1]
        workarr[:,:-1] += tmp[:,1:]
        workarr >>= 2


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org