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

Re: [pygame] ChangeColor Routine



I think this below works.  It seems to be the fastest too.

Unfortunately putmask seemed to want a contiguous array, so I changed
that with the line:

    array_new = Numeric.array(array)

Numeric is a bit weird sometimes.


def replace_color(pic, oldcolor, newcolor):

    oldcolor_mapped = pic.map_rgb(oldcolor)
    newcolor_mapped = pic.map_rgb(newcolor)

    array = pygame.surfarray.pixels2d(pic)

    array_new = Numeric.array(array)

    mask = Numeric.where(array_new == oldcolor_mapped, 1, 0)
    Numeric.putmask(array_new, mask, newcolor_mapped)







On Sun, 13 Feb 2005 17:05:26 +0200, Sami Hangaslammi
<sami.hangaslammi@xxxxxxxxx> wrote:
> On Sun, 13 Feb 2005 14:03:04 +1100, Rene Dudfield <renesd@xxxxxxxxx> wrote:
> > I thought this might work.  But == does not work on the [1,1,1].  It
> > only works on the each element.  Not sure how to make it do that.
> >
> > def abrokenway(pic, oldcolor, newcolor):
> >     array = pygame.surfarray.pixels3d(pic)
> >     mask = Numeric.where(array == oldcolor, 1, 0)
> >     Numeric.putmask(array, mask, newcolor)
> 
> I think you could use Surface.map_rbg to get the value for oldcolor
> and newcolor and then assign them using pixels2d instead of 3d.
> 
> An to the original poster: The surfarray module isn't any faster than
> get_at and set_at if you are setting and testing single pixels. The
> speed increase is attained by eliminating Python loops using Numeric's
> operators and special functions.
> 
> --
> Sami Hangaslammi
>