[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] about array_alpha?
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] about array_alpha?
- From: Jack Nutting <jnutting@xxxxxxxxx>
- Date: Mon, 1 Aug 2005 15:44:34 +0200
- Delivered-to: archiver@seul.org
- Delivered-to: pygame-users-outgoing@seul.org
- Delivered-to: pygame-users@seul.org
- Delivery-date: Mon, 01 Aug 2005 09:44:48 -0400
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=uSv3ZC73EilFcRPcBz8UrDpjBhTrO6sIZQkUrlyYxP8+LNfcm4EPc7BE5VDWXSzooyA5Z4MhggafOmEpP3p+jZIfjvrYPHr9AT0ZqMsJOpyZN+qf/XtOEx1OYiChi8Bdz6BCZevO0MwSpnUeGX9Loj0nOIhueZJVtU39SIqtjrg=
- In-reply-to: <00fa01c595d2$b8ee8f80$fac588de@pppzmoxa4dgdut>
- References: <00fa01c595d2$b8ee8f80$fac588de@pppzmoxa4dgdut>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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