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

Re: Re: [pygame] surfarray woes



I was unable to make work:

pygame.surfarray.array3d(surf).as_type(Int)
AttributeError: as_type

but I was able to make work:

N.array(pygame.surfarray.array3d(surf),N.Int16)

But this seems to be doing a lot of work.  I end up creating 3 arrays just to do a blur (I know that two of them are required ;) 

off-topic: I have been using Matlab for my numerical linear algebra class, and it seems that Numeric+Python is a good substitute!

Thanks for the swift reply to my first question.

---

Has anyone created a XScreensaver using pygame?  Anyone have a good idea for grabbing a screenshot of the screen before launching fullscreen pygame display?  

Jesse Andrews

-----Original Message-----
From: Pete Shinners <shredwheat@attbi.com>
To: pygame-users@seul.org
Date: Mon, 30 Sep 2002 00:08:01 -0700
Subject: 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



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