How can I make an Image with a transparent Backround in Pygame?

The issue is the image format. JPEG images have no alpha channel. You can set a transparent colorkey by set_colorkey(), but the result will not satisfy you, because the JPEG is not lossless. That means due the compression, the colors will slightly change and the color key will not work correctly. For instance white color:

self.image = image = pygame.image.load("mew.jpg")
self.image.set_colorkey((255, 255, 255))

Either use set_colorkey() and a lossless image format like BMP:

self.image = image = pygame.image.load("mew.bmp")
self.image.set_colorkey((255, 255, 255))

or us the PNG format. For instance:

image = pygame.image.load("mew.png").convert_alpha()

Leave a Comment