Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Surface.copy() sometimes loses transparency

Tags:

python

pygame

For some (but not all!) images, copying a surface using surface.copy() loses the transparency. So I've got two questions?

  1. Why does copy lose the transparency? The docs sound like everything about the new surface should be the same, but that's obviously not happening.

  2. Why does this happen with some images and not others?

Here is an example "bad" image -- when copied, the transparency is lost

Bad image.

Here is an example "good" image -- when copied, the transparency is not lost.

Good image.

And here is the code that you can run to see the difference:

import pygame

def test():
    screen = pygame.display.set_mode((320, 240))
    bad_original = pygame.image.load('bad-image.gif') 
    bad_copied = bad_original.copy()

    good_original = pygame.image.load('good-image.gif') 
    good_copied = good_original.copy()

    while True:
        for event in pygame.event.get():
            if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE or
                event.type == pygame.QUIT):
                pygame.quit()

        screen.fill((150, 150, 150))

        screen.blit(bad_original, (0,0))
        screen.blit(bad_copied, (100, 0))
        screen.blit(good_original, (0,100))
        screen.blit(good_copied, (100, 100))
        pygame.display.flip()

if __name__ == '__main__':
    test()

And heck, for completion, here's what a screenshot of running the above code looks like. Result of running the above code.

Please note that I'm not looking for workarounds; I just want to know what I am not understanding about surface.copy, or anything you think I may not understand about working with Pygame surfaces.

I'm using Python 3.3 and Pygame 1.9.2pre on a Windows 7 machine.

like image 226
Cody Piersall Avatar asked Feb 13 '26 21:02

Cody Piersall


1 Answers

You need to use .convert_alpha()

Try:

pygame.image.load('my_image.gif').convert_alpha()

See: http://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert_alpha

"Creates a new copy of the surface with the desired pixel format. The new surface will be in a format suited for quick blitting to the given format with per pixel alpha. If no surface is given, the new surface will be optimized for blitting to the current display.

Unlike the Surface.convert() method, the pixel format for the new image will not be exactly the same as the requested source, but it will be optimized for fast alpha blitting to the destination."

In pygame anytime you load and image, or create a surface, with the intent of displaying it you should .convert() it if it has no transparency, or .convert_alpha() it if it has transparency. This yields both a big speedup AND solves the mystery of, 'Why is my transparency doing that?'.

like image 73
James Spencer Avatar answered Feb 20 '26 12:02

James Spencer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!