[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] surfarray & alpha



mazer@socrates.berkeley.edu wrote:

> Hi Pete,
> 
> That doesn't seem to work for me.  Here's a version along the lines of
> what you suggested (the surface being changed is a 32bit surface
> generated by a call to convert_alpha() ("<Surface(100x31x32 SW)>"):

i've attached a quick demo that shows this working.
i'm guessing perhaps you have a 32bit surface, but don't
have the "SRCALPHA" flag enabled for it.

it just "fades out" an image by changing the pixel alpha
for every other row. no frills, but its working :]


this does bring to light the need for Surface.convert()
to better handle pixel alphas, like the Surface() func
can. ahwell, its easy enough in this case to just pass
the desired masks for each color plane.

import pygame, pygame.surfarray, pygame.image
from pygame.locals import *

pygame.init()


image = 'chimp.gif' #change to any image

alphamasks = 0xff<<16, 0xff<<8, 0xff, 0xFF<<24
black = 0,0,0
white = 255,255,255


#surfaces
screen = pygame.display.set_mode((200, 200))
img = pygame.image.load('chimp.gif').convert(alphamasks)
alpha = pygame.surfarray.pixels_alpha(img)


#doublecheck
print "Surface Masks:", img.get_masks()
print "Has SRCALPHA :", ['no','yes'][img.get_flags()&SRCALPHA!=0]


#positioning info
screenrect = screen.get_rect()
pos = img.get_rect()
pos.center = screenrect.center
tophalf = Rect(screenrect)
bottomhalf = Rect(screenrect)
tophalf.bottom = screenrect.centery
bottomhalf.top = screenrect.centery


#loop over different alphas
for x in range(0, 255, 5):
    screen.fill(black, tophalf)
    screen.fill(white, bottomhalf)

    alpha[::2] = 255 - x
    screen.blit(img, pos)

    pygame.display.update()
    pygame.time.delay(100)


pygame.time.delay(600)