[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] using surfarrays without losing alpha?
Say I've got an image I want to "soften". I've already got a handy
function using surfarrays, taken from example code:
def softenRgbArray(rgbarray):
""" shamelessly lifted from arraydemo.py"""
soften = Numeric.array(rgbarray)*1
soften[1:,:] += rgbarray[:-1,:]*8
soften[:-1,:] += rgbarray[1:,:]*8
soften[:,1:] += rgbarray[:,:-1]*8
soften[:,:-1] += rgbarray[:,1:]*8
soften /= 33
return soften
I just want to wrap this with another function that will take a surface
and return the softened surface. Here's a first stab at it:
def softenImage(img):
rgbarray = pygame.surfarray.array3d(img)
transformedArray = softenRgbArray(rgbarray)
return pygame.surfarray.make_surface(transformedArray)
This works, with one problem: All alpha data from the original image
is lost! At this point, I figure the best thing to do is to make a new
surface that is a copy of the old one, and first blit the original
image (including alpha) into the new surface, followed by the
transformed color array, like this:
def softenImage(img):
rgbarray = pygame.surfarray.array3d(img)
transformedArray = softenRgbArray(rgbarray)
newImg = pygame.Surface(img.get_size()).convert_alpha()
newImg.blit(img, newImg.get_rect())
pygame.surfarray.blit_array(newImg, transformedArray)
return newImg
Unfortunately, this just gives me surfaces that are solid black for
some reason! I've been abusing and contorting this code for a while to
try to make it work... Can anyone see an obvious (or not-so-obvious)
error here?
//jack