[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Saving Alpha Channels



Hi,

I'm running into random troubles with PyOpenGL transparency again, and the simplest solution is just to make the textures have transparency.  Normally, I would just add the transparency in an image editor, but unfortunately I have ~250 images.  My solution is programming!  I wrote a script which should convert all the images to be ~50% transparent:

import pygame
from pygame.locals import *
import sys, os
pygame.init()
Surface = pygame.display.set_mode((256,256))
Images = []
for x in xrange(1,250,1):
    numstr = str(x)
    while len(numstr) < 4:
        numstr = "0"+numstr
    img = pygame.Surface((256,256))
    img.blit(pygame.image.load(str(x)+".jpg").convert(),(0,0))
    img.set_alpha(128)
    Images.append(img)
    pygame.image.save(img,str(x)+".png")
frame = 0
Clock = pygame.time.Clock()
while True:
    Surface.fill((0,0,0))
    Surface.blit(Images[frame],(0,0))
    frame = (frame+1)%len(Images)
    pygame.display.flip()
    Clock.tick(50)

Now, what I see here is a window open and then a nice animation of the images playing across it.  The images are darker, because I am seeing the black (Surface.fill((0,0,0))) through the images.  Changing the alpha value changes the brightness.  Unfortunately, the saved images do not have the alpha channel, which means that they don't have transparency which means that the textures don't work.

Ideas?

Ian