Hi all. :)

I'm having trouble with pygame, images and transparency.
Lets say I have an image...a circle to keep it simple (for example only).

What I want is to be able to draw the image, but with only the circle showing (ie. not a white box around it). Oh and it's png (png ftw :D).

I've tried the following

#Firstly:

image = pygame.image.load('example.png')
image.convert_alpha()

#it was suggested to me by someone, but I didn't think it would work...but it didn't.

#Secondly:

image = pygame.image.load('example.png')
image.set_colorkey((255, 255, 255)) #White, for example
#Fail -_-

#Thirdly:

'''
I had been using paint, but I went on Paint.net, and cut out around the main image, and deleted the rest to get the checkbox (transparent) thingy. Then I loaded this pic. It didn't work.
'''

I think colorkey is what I need, but I'm not sure how to use it on images, cos I could only find a thing on using it with surfaces.

Can anyone show me how to get transparent image backgrounds? O_o

Thanks
Mark

Recommended Answers

All 2 Replies

convert_alpha() returns a new surface that you need to save over your old one.

image = pygame.image.load('example.png')
    image = image.convert_alpha()

It's much more efficient than setting a colorkey because it converts the image to Pygame's native blitting format.

oh okay thanks i will try that later. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.