How do I blit a PNG with some transparency onto a surface in Pygame?

Eric picture Eric · Oct 28, 2009 · Viewed 18.6k times · Source

I'm trying to blit a PNG image onto a surface, but the transparent part of the image turns black for some reason, here's the simple code:

screen = pygame.display.set_mode((800, 600), pygame.DOUBLEBUF, 32)

world = pygame.Surface((800, 600), pygame.SRCALPHA, 32)
treeImage = pygame.image.load("tree.png")

world.blit(treeImage, (0,0), (0,0,64,64))
screen.blit(world, pygame.rect.Rect(0,0, 800, 600))

What do I have to do to solve the problem? The image has alpha transparency, I've opened it up in PhotoShop and the background turns transparent, not black or white or any other color.

Thank you for your support :)

Answer

Nick T picture Nick T · Oct 28, 2009

http://www.pygame.org/docs/ref/image.html recommends:

For alpha transparency, like in .png images use the convert_alpha() method after loading so that the image has per pixel transparency.