How to convert the background color of image to match the color of Pygame window?

You don’t need to change the background color of the image to the background color of the window, but make the background of the image transparent.


Set the transparent colorkey by pygame.Surface.set_colorkey:

Set the current color key for the Surface. When blitting this Surface onto a destination, any pixels that have the same color as the colorkey will be transparent.

Note, all the background has to have exactly the same color. In your case the background color seems to be gray (230, 230, 230):

self.image = pygame.image.load('images/ship.bmp').convert()
self.image.set_colorkey((230, 230, 230))

Another option wound be to create a new image (you need to draw it in a painting application) with per pixel alpha (e.g. PNG) and a transparent background:

self.image = pygame.image.load('images/ship.png').convert_alpha()

Leave a Comment