How can I color a Pygame image surface? I either want to change every pixel that is color A into color B, or else change every pixel into color B. Either works, so long as transparent regions remain the same.
Essentially what a surfarray does is directly modify the pixel values of pygame surfaces, and can operate on each of the R, G, and B channels for every pixel "simultaneously," which I put in quotes because I just mean you can change all the pixels with just one line of code. Ordinarily, this would be a very slow operation, but surfarrays use numpy/numeric as their core, so the operation is quite fast. I should still note that in practice, I've found it not to be fast enough for some applications, such as operating on large images, but if you're only operating on small particle sprites, you should be fine.
I did a bit of toying around to make sure that this works as you need it. Below is a code example of what you're looking to do. Some credit goes to the poster who answered this question.
def color_surface(surface, red, green, blue): arr = pygame.surfarray.pixels3d(surface) arr[:,:,0] = red arr[:,:,1] = green arr[:,:,2] = blue
In that function, I use
pygame.surfarray.pixels3d because it references the pixels into an array, rather than copy them, making it a faster operation. The tutorial I posted also uses an additional instruction, something like:
iarray = numpy.array(arr)
which I also left out because I've found this to be a startlingly slow call in practice. It might be necessary if you try to do more complicated things with the surface.
So, the last few steps are actually taking your original surface and coloring it the way you need:
origSurface = pygame.image.load('images/blue_circle.png') origSurface.convert_alpha() coloredSurface = origSurface.copy() color_surface(coloredSurface, 120, 78, 240)
A couple of notes about this code: the call to
convert_alpha on the surface seems to be unnecessary if the image already has per-pixel alphas defined. The StackOverflow question that I referenced details the two ways to ensure a surface has alphas which are unaffected by surfarray operations. Also, copying a surface may not necessarily preserve the alphas, but like the last point, I've found that it does when the alphas are already defined in the original image. Just a few things to watch out for.
This ended up rather long... One last note... If you're feeling adventurous, you can look into the gfxdraw libraries for pygame. The
pygame.gfxdraw.textured_polygon function caught my attention. I know very little about how it works or it's performance though.
Hope this helps :)
ktodisco's solution is good but requires numpy to be installed. I wanted my game to require no extra dependencies beyond Python and PyGame, so I searched for another solution and was inspired by this answer to a related question.
This routine only requires a couple calls to fill() with blend flags:
def colorize(image, newColor): """ Create a "colorized" copy of a surface (replaces RGB values with the given color, preserving the per-pixel alphas of original). :param image: Surface to create a colorized copy of :param newColor: RGB color to use (original alpha values are preserved) :return: New colorized Surface instance """ image = image.copy() # zero out RGB values image.fill((0, 0, 0, 255), None, pygame.BLEND_RGBA_MULT) # add in new RGB values image.fill(newColor[0:3] + (0,), None, pygame.BLEND_RGBA_ADD) return image