Using Surface.copy() sometimes loses transparency
Question: 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](http://www.pygame.org/docs/ref/surface.html) 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.](http://i.stack.imgur.com/aaOwr.gif)

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

![Good image.](http://i.stack.imgur.com/vgXM5.gif)

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.](http://i.stack.imgur.com/UQtK2.png)

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.


Answer: 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?'.

