Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 :)

like image 213
Eric Avatar asked Oct 27 '09 23:10

Eric


People also ask

Does PNG support partial transparency?

Test inline PNG images PNG images can contain alpha (transparency) information. Unlike GIF, which requires a particular color to be designated fully transparent, PNG allows the transparency of all pixels to take any value from fully transparent though partial transparency to full opacity.

Does pygame support transparency?

There are three types of transparency supported in pygame: colorkeys, surface alphas, and pixel alphas. Surface alphas can be mixed with colorkeys, but an image with per pixel alphas cannot use the other modes. Colorkey transparency makes a single color value transparent.


1 Answers

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.

like image 131
Nick T Avatar answered Oct 05 '22 04:10

Nick T