Search code examples
pythonpygamepixel

Keep Alpha in Pygame PixelArray


I wan't to change every colors in my image to grey;but I don't wan't to make any changes to my image alpha.I wan't to keep transparent pixels of my image.this is my code:

from pygame import *

init()

screen = display.set_mode([640, 640])

r = image.load('g.png')
r = transform.scale(r,(640,640))

ar = PixelArray (r)
ar.replace ((255, 255, 255), (110, 110, 110), 0.15)
del ar

screen.blit(r, (0,0))

display.update()

Solution

  • Here's a workaround: Paint all transparent pixels of your image-file g.png with a color you're never going to use, like magic-pink or pitch-black. Then just change ever color of the image but you're transparent color - and then, before you blit, convert your array to a surface and set the colorkey to your transparent color, like this: surface.set_colorkey((225, 0, 225))