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

Re: [pygame] Surfaces with Transparent Backgrounds



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.nakashima@xxxxxxxxx> 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 <pymike93@xxxxxxxxx> wrote:
Hi,

I have a function that loads image strips and 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