Re: [pygame] Surfaces with Transparent Backgrounds

2009-08-21 Thread Rolf Sievers

Hi, I don't know Pygame that well and have never used subsurfaces
but it it looks kinda interesting. Would you do the subsurface
approach in the following way?

def load_strip(filename, width):
  imgs = []
  img = load_image(filename)
  for x in range(img.get_width()/width):
  i = img.subsurface(pygame.Rect(-x*width, 0, width, img.get_height()))
  imgs.append(i)
  return imgs


Patrick Mullen schrieb:

Lenard: his problems was that he was blitting that per-pixel alpha
surface onto a newly created surface which also had to be per-pixel
alpha, and wasn't sure how to create that. But he figured it out :)



Pymike, that's one way to do it, another way would be to use
subsurfaces.  They are a bit nicer because you don't have to go
through the hassle of making a separate surface for each frame.  I
think they might take less memory as well.


On Thu, Aug 20, 2009 at 8:47 PM, Lenard Lindstromle...@telus.net wrote:
  

Hi pymike,

I don't understand what you are doing here. If you load an image with
per-pixel alpha, the returned surface will also have per-pixel alpha. All
convert_alpha() may do here is improve performance by formating the surface
to match the display. Of course this is with Pygame 1.9.1. Earlier Pygames
may have a problem with 32bit to 32bit surface blits.

Lenard Lindstrom

pymike wrote:


Aha! Figured it out!

def load_strip(filename, width):
   imgs = []
   img = load_image(filename)
   for x in range(img.get_width()/width):
*i = pygame.Surface((width, img.get_height())).convert_alpha()
   i = i.convert_alpha()
   i.fill((255,255,255,0))*
   i.blit(img, (-x*width, 0))
   imgs.append(i)
   return imgs

Everything's working awesome now.

--
- pymike
  



[pygame] Surfaces with Transparent Backgrounds

2009-08-20 Thread pymike
Hi,

I have a function that loads image
stripshttp://pymike.pynguins.com/downloads/player-run.pngand returns
the images in lists. To do this, I create new surfaces and blit
the strips to them at an offset.

My question is: Is there a way to create surfaces with transparent
backgrounds? The images have quite a bit of semi-transparent pixels in them,
so I can't use colorkeys.

Thanks,

-- 
- pymike


Re: [pygame] Surfaces with Transparent Backgrounds

2009-08-20 Thread Henrique Nakashima
Transparency is often a problem because image editors and viewers not always
display the same sorts of transparency.

To create a transparent background, load it from an image with transparency.
Png supports it, but not all png editors do. I managed to get transparency
done by using GIMP (http://www.gimp.org/). In this editor, you need to add a
transparency channel to the image, select the part that you want to be
transparent and delete it (with the Delete key). This usually does the
trick, but people more experient with image editing might have better ways.

Pygame can also do transparency by using color keys, which means defining a
specific RGB color that will represent transparent pixel.  In your example,
that would be white (0, 0, 0). Use
http://www.pygame.org/docs/ref/surface.html#Surface.set_colorkey for this
kind of transparency.

On Thu, Aug 20, 2009 at 22:46, pymike pymik...@gmail.com wrote:

 Hi,

 I have a function that loads image 
 stripshttp://pymike.pynguins.com/downloads/player-run.pngand returns the 
 images in lists. To do this, I create new surfaces and blit
 the strips to them at an offset.

 My question is: Is there a way to create surfaces with transparent
 backgrounds? The images have quite a bit of semi-transparent pixels in them,
 so I can't use colorkeys.

 Thanks,

 --
 - pymike



Re: [pygame] Surfaces with Transparent Backgrounds

2009-08-20 Thread pymike
I *did* create the images with Gimp, and they do have transparent
backgrounds. (Did you view the image link I posted?)

I have used colorkeys in the past, but the problem here is that there are
semi-transparent (not fully opaque) pixels in the images (again see the
image I linked to), and when you draw these images to newly created surfaces
(ie new = pygame.Surface(); new.blit(loadedimage)) the pixels are no longer
transparent. Hence why I need to create surfaces with completely transparent
backgrounds, instead of the default black.

Here's my code for reference:

import os

import pygame
from pygame.locals import *

IMAGE_CACHE = {}

def load_image(filename):
if filename not in IMAGE_CACHE:
img = pygame.image.load(os.path.join(data,
filename)).convert_alpha()
IMAGE_CACHE[filename] = img
return IMAGE_CACHE[filename]

def load_strip(filename, width):
imgs = []
img = load_image(filename)
for x in range(img.get_width()/width):
*i = pygame.Surface((width, img.get_height()))***
*i.blit(img, (-x*width, 0))*
imgs.append(i)
return imgs

Cheers,

On Thu, Aug 20, 2009 at 9:05 PM, Henrique Nakashima 
henrique.nakash...@gmail.com wrote:

 Transparency is often a problem because image editors and viewers not
 always display the same sorts of transparency.

 To create a transparent background, load it from an image with
 transparency. Png supports it, but not all png editors do. I managed to get
 transparency done by using GIMP (http://www.gimp.org/). In this editor,
 you need to add a transparency channel to the image, select the part that
 you want to be transparent and delete it (with the Delete key). This usually
 does the trick, but people more experient with image editing might have
 better ways.

 Pygame can also do transparency by using color keys, which means defining a
 specific RGB color that will represent transparent pixel.  In your example,
 that would be white (0, 0, 0). Use
 http://www.pygame.org/docs/ref/surface.html#Surface.set_colorkey for this
 kind of transparency.


 On Thu, Aug 20, 2009 at 22:46, pymike pymik...@gmail.com wrote:

 Hi,

 I have a function that loads image 
 stripshttp://pymike.pynguins.com/downloads/player-run.pngand returns the 
 images in lists. To do this, I create new surfaces and blit
 the strips to them at an offset.

 My question is: Is there a way to create surfaces with transparent
 backgrounds? The images have quite a bit of semi-transparent pixels in them,
 so I can't use colorkeys.

 Thanks,

 --
 - pymike





-- 
- pymike


Re: [pygame] Surfaces with Transparent Backgrounds

2009-08-20 Thread Ian Mallett
You need to convert the surface i with .convert_alpha(), as well.


Re: [pygame] Surfaces with Transparent Backgrounds

2009-08-20 Thread pymike
On Thu, Aug 20, 2009 at 10:15 PM, Ian Mallett geometr...@gmail.com wrote:

 You need to convert the surface i with .convert_alpha(), as well.


i = pygame.Surface((width, img.get_height())).convert_alpha()

...I still get a black background.

-- 
- pymike


Re: [pygame] Surfaces with Transparent Backgrounds

2009-08-20 Thread pymike
Aha! Figured it out!

def load_strip(filename, width):
imgs = []
img = load_image(filename)
for x in range(img.get_width()/width):
*i = pygame.Surface((width, img.get_height())).convert_alpha()
i = i.convert_alpha()
i.fill((255,255,255,0))*
i.blit(img, (-x*width, 0))
imgs.append(i)
return imgs

Everything's working awesome now.

-- 
- pymike


Re: [pygame] Surfaces with Transparent Backgrounds

2009-08-20 Thread Henrique Nakashima
Aha.

There might be something weird about the image as well, because when I used
another one with a transparent background it worked as well - without
modifications to the code. Despite that, I couldn't find anything strange in
the png.

On Fri, Aug 21, 2009 at 00:23, pymike pymik...@gmail.com wrote:

 Aha! Figured it out!

 def load_strip(filename, width):
 imgs = []
 img = load_image(filename)
 for x in range(img.get_width()/width):
 *i = pygame.Surface((width, img.get_height())).convert_alpha()
 i = i.convert_alpha()
 i.fill((255,255,255,0))*
 i.blit(img, (-x*width, 0))
 imgs.append(i)
 return imgs

 Everything's working awesome now.

 --
 - pymike



Re: [pygame] Surfaces with Transparent Backgrounds

2009-08-20 Thread Lenard Lindstrom

Hi pymike,

I don't understand what you are doing here. If you load an image with 
per-pixel alpha, the returned surface will also have per-pixel alpha. 
All convert_alpha() may do here is improve performance by formating the 
surface to match the display. Of course this is with Pygame 1.9.1. 
Earlier Pygames may have a problem with 32bit to 32bit surface blits.


Lenard Lindstrom

pymike wrote:

Aha! Figured it out!

def load_strip(filename, width):
imgs = []
img = load_image(filename)
for x in range(img.get_width()/width):
*i = pygame.Surface((width, img.get_height())).convert_alpha()
i = i.convert_alpha()
i.fill((255,255,255,0))*
i.blit(img, (-x*width, 0))
imgs.append(i)
return imgs

Everything's working awesome now.

--
- pymike