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

Re: [pygame] ChangeColor Routine



Hrm, that didn't work for me, and seems calculate the results without updating the image. The following code works for me; it's basically the same with the addition of the last line.

-Jasper

PS I pleasantly surprised that this even works with non-indexed images!


def swapRGBA( image, sourceRGBA, destRGBA ): '''swap RGBA; A is assumed to be 255 if missing''' pixels = pygame.surfarray.pixels2d( image ) mask = Numeric.where( pixels == image.map_rgb( sourceRGBA ), 1, 0 )

   newPixels = Numeric.array( pixels )  # Hack contiguity for putmask
   Numeric.putmask( newPixels, mask, image.map_rgb( destRGBA ) )
   pixels[:] = newPixels


Rene Dudfield wrote:

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)