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

Re: [pygame] about array_alpha?



On 7/31/05, flyaflya <flyaflyaa@xxxxxxxxx> wrote:
>  
> "pygame.surfarray.array_alpha" can be use to get a surface alpha array, but
> how to set this alpha array return to the surface?  
> I know "make_surface" can make a surface form rgb array, but I cant find how
> to use alpha array? 

The technique that works for me is to use
pygame.surfarray.pixels_alpha() to get a direct "pointer" to the alpha
pixel array for an existing image, and then copy your own data into
that.

The method I've found for doing this involves jumping through a few
hoops, but it's not too bad.  Here's an extra-annotated code snippet
from one of my projects.  This assumes the existence of a function
called softenImageArray() (which I came across in arraydemo.py)

def superSoftenImage(img):
    """ just a wrapper for softenRgbArray.  this softens both color and
    alpha channel (so that edges of objects will be gradually smoothed out). """

    # take the rgb data and soften it
    rgbarray = pygame.surfarray.array3d(img)
    transformedArray = softenImageArray(rgbarray)

    # create new image from the transformed rgb data, and give it an
alpha channel
    newImg = pygame.surfarray.make_surface(transformedArray).convert_alpha(img)

    # take the alpha data and soften it
    origAlphaArray = pygame.surfarray.array_alpha(img)
    softenedAlphaArray = softenImageArray(origAlphaArray).astype(
origAlphaArray.typecode())

    # grab a hook into the alpha pixel data of newImg
    newImgAlphaArray = pygame.surfarray.pixels_alpha(newImg)

    # and put the softened alpha data in there
    newImgAlphaArray[:] = softenedAlphaArray

    return newImg


-- 
// jack
// http://www.nuthole.com