2

I'm able to set an image to be transparent using the both the set_colorkey method and the convert_alpha method over a surface background. However, when I try to overlap two images the transparency is lost for some reason.

So to reiterate, is there a way to make part of an image transparent over another image (e.g. two png or gif files)

  • 1
    Can you post some code? Are you blitting both images on the background, or one over the other and then over the background? How are you defining the surfaces and loading the images? – Gustavo Giráldez Jun 24 '11 at 11:35
  • Are you blitting from one surface to the other, or both to screen? – ninMonkey Sep 22 '11 at 03:59

1 Answers1

0

I'm not sure if you're running into "buggy" behavior or not. It is possible color-key alpha doesn't support what you are trying to do, and it is possible that you are either defining your pixel data wrong, or that there is some sort of bug (video driver or pygame) that you're experiencing.

But I do have a work-around for you:

http://www.pygame.org/docs/ref/surface.html

There are three types of transparency supported in Pygame: colorkeys, surface alphas, and pixel alphas

You could try pixel alphas. It will be more flexible than color-key alpha, and should support blending as many layers as you want.

You can do this by passing SRCALPHA when creating the surface, and ensuring that your input pixel data has a properly defined alpha channel.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • Okay, this may be a newbie type question....I understand that you can pass `SRCAPLHA` to a surface like this `image = pygame.Surface([640,480], SRCALPHA, 32)` but how would you do it for an image that you load `image = pygame.image.load(filename)`. It seems like the only parameter it takes is the file name, right? –  Jun 24 '11 at 01:56
  • @sq1020: Just format your file correctly (create the alpha channel correctly in your paint program), and hopefully pygame does the heavy lifting for you. If not, then sorry I can't help. I haven't worked with pygame, just SDL (which has a similar API for such things), and it's been a few years since I've done that :) – Merlyn Morgan-Graham Jun 24 '11 at 01:59
  • @sq1020: But if this option doesn't work out, let me know and I'll delete it so as not to scare away new answers. – Merlyn Morgan-Graham Jun 24 '11 at 02:02
  • Right, I did that already and the image is transparent against the surface background but it's not against other images. So it does work but just not against other images (both colorkeys and pixel alphas). Have you ever run into this specific problem in SDL? –  Jun 24 '11 at 02:09
  • @sq1020: Probably a few times, but it's been a while. This page mentions making extra calls to enable alpha transparency: http://pygame.org/docs/ref/image.html . In particular, "use the `convert_alpha()` method after loading so that the image has per pixel transparency". You might also want to try different options in `pygame.display`, or `pygame.display.set_mode()` - http://pygame.org/docs/ref/display.html – Merlyn Morgan-Graham Jun 24 '11 at 02:30