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

Re: [pygame] Newbie surfarray question

> Is there a fast possibility to add/substract 2 surfarrays
> (for light/shadow effects) without getting those ugly effect.
> (if the value would be graeter  than 255)
> i finally got it working, but it is too slow
> for i in range(x):
>     for j in range(y):
>         for k in range(3): # RGB
>             a = long(imgarray[i,j,k]) + long(i2array[i,j,k])
>             if a > 255:
>                 a = 255
>             imgarray[i,j,k] = a

the secret is that you never want to loop through each individual
index with Numeric and surfarrays. the performance will never be
good. fortunately, there are plenty of ways to get what you want
without looping through like this. here are your funcs

def addarrays(a, b):
    data = a.astype('i') #need int for no overflow
    add(data, b, data)   #add b into the data array
    return minimum(data, 255).astype('b')

there you go. if you are just going to be calling blit_array on
the result, there's no need to worry about that final astype('b').
this code will be nearly as fast as you can do it in C.

pygame mailing list