Re: [pygame] Saving Alpha Channels
Well, you could try getting the alpha array via pygame.surfarray.pixels_alpha and then running: alphas[...] = 128 # Sets all alpha values to 128. Might have to run it through Surface.convert_alpha before it's got an alpha array to work with, though. -FM On Sat, Oct 4, 2008 at 11:47 PM, Ian Mallett [EMAIL PROTECTED] wrote: On Sat, Oct 4, 2008 at 8:51 PM, Brad Montgomery [EMAIL PROTECTED] wrote: Would something like PIL's putalpha do what you want? (provided you *have* PIL) Yes, I have PIL. http://www.pythonware.com/library/pil/handbook/image.htm (search for putalpha) import Image my_image = Image.open('somefile.jpg') my_image.put_alpha(0) my_image.save('somefile.png') This would convert all the black (int val for black = 0) in your image to alpha's, but then you'd have to save to something that supports an alpha channel. -- brad [bradmontgomery.net] Well, I want to set the alpha value for every pixel. So, every pixel, regardless of its color should be 50% transparent. The code I posted does that, but it doesn't save that alpha data to the file. Ian
Re: [pygame] Saving Alpha Channels
Ian Mallet: 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: Well, here is how you can fill a surface with alpha values in pygame: import pygame screen = pygame.display.set_mode([400,400]) img = pygame.Surface([400,400]).convert_alpha() img.fill([255,255,255]) array = pygame.surfarray.pixels_alpha(img) array[:,:] = 50 del array pygame.image.save(img,test.png) surface.set_alpha does NOT fill the surface with alpha values, all it does is tell the rendering system to mix the colors when it blits the surface. This is why it won't save those values. But what trouble are you having with transparency in pyopengl? If you use glColor to set an alpha value, you should get the same effect. And it will be more flexible too, such as allowing you to handle things fading in etc. Although I don't know what your problem space is. But if you are having issues such as objects that should be in front displaying behind etc, I don't think making the textures have alpha is going to fix the problem. I think we should look at the original problem instead of trying to work around it.
Re: [pygame] Saving Alpha Channels
On Sat, Oct 4, 2008 at 11:59 PM, Patrick Mullen [EMAIL PROTECTED]wrote: Ian Mallet: Mallett ;-) 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: Well, here is how you can fill a surface with alpha values in pygame: import pygame screen = pygame.display.set_mode([400,400]) img = pygame.Surface([400,400]).convert_alpha() img.fill([255,255,255]) array = pygame.surfarray.pixels_alpha(img) array[:,:] = 50 del array pygame.image.save(img,test.png) Cool, this works. Thank you. surface.set_alpha does NOT fill the surface with alpha values, all it does is tell the rendering system to mix the colors when it blits the surface. This is why it won't save those values. OK. But what trouble are you having with transparency in pyopengl? If you use glColor to set an alpha value, you should get the same effect. And it will be more flexible too, such as allowing you to handle things fading in etc. Although I don't know what your problem space is. But if you are having issues such as objects that should be in front displaying behind etc, I don't think making the textures have alpha is going to fix the problem. I think we should look at the original problem instead of trying to work around it. Naturally, that was my original ideology--simpler is better. I look for causes of bugs, not workarounds. Of course, in this case, the problem is more complex--the texture is to be mapped onto a surface which will be shadowed by at least three lights casting three separate shadows onto a single object via a total 9-pass shadowmapping algorithm. Whew. So much for simple... It's actually not as weird as that sounds, and I'm sure it could be done in PyOpenGL, but in this case, I think the simpler solution is just to load the transparency in the transparency channel. In short: the texture could be made transparent through PyOpenGL, but that makes it more complex... So, thank you very much, everyone! Ian
[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
Re: [pygame] Saving Alpha Channels
On Sat, Oct 4, 2008 at 10:34 PM, Ian Mallett [EMAIL PROTECTED] wrote: [snip]... 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? Would something like PIL's putalpha do what you want? (provided you *have* PIL) http://www.pythonware.com/library/pil/handbook/image.htm (search for putalpha) import Image my_image = Image.open('somefile.jpg') my_image.put_alpha(0) my_image.save('somefile.png') This would convert all the black (int val for black = 0) in your image to alpha's, but then you'd have to save to something that supports an alpha channel. -- brad [bradmontgomery.net]
Re: [pygame] Saving Alpha Channels
On Sat, Oct 4, 2008 at 8:51 PM, Brad Montgomery [EMAIL PROTECTED]wrote: Would something like PIL's putalpha do what you want? (provided you *have* PIL) Yes, I have PIL. http://www.pythonware.com/library/pil/handbook/image.htm (search for putalpha) import Image my_image = Image.open('somefile.jpg') my_image.put_alpha(0) my_image.save('somefile.png') This would convert all the black (int val for black = 0) in your image to alpha's, but then you'd have to save to something that supports an alpha channel. -- brad [bradmontgomery.net] Well, I want to set the alpha value for every pixel. So, every pixel, regardless of its color should be 50% transparent. The code I posted does that, but it doesn't save that alpha data to the file. Ian