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

[pygame] More elegant colorize routine?



HandyGM wrote:
Hi there. I have code to colorize a 256 color surface instantly... now I need to create a routine that will colorize a full color surface.

Can anyone tell me why this won't work?

        pic    = pygame.image.load('MyFullColorPic.png')
        adj    = (50, 50, 50)
        array  = pygame.surfarray.array3d(pic)
        array  = array * (255+adj[0], 255+adj[1], 255+adj[2])
        array /= 255
        Numeric.clip(array, [0, 0, 0], [255, 255, 255])
        pygame.surfarray.blit_array(pic, array)


This works, but I bet it could benefit from a handy Numeric shortcut. Anyone know how to make this more elegant using Numeric?

        array  = pygame.surfarray.array3d(pic)
        for i in range(len(array)):
            for j in range(len(array[i])):
                for k in range(3):
                    num = array[i][j][k] + adj[k]
                    if num < 0:
                        num = 0
                    elif num > 255:
                        num = 255
                    array[i][j][k] = num
        pygame.surfarray.blit_array(pic, array)