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

Re: [pygame] Problems with surfarrays



> Well, I tried doing it this way, however any changes I make to alphaarray
> don't seem to affect the picture at all.


hmm, perhaps this is best explained with an example?
attached is a fun little program that takes an image with solid RGB 
values, and turns it into an image of horizontal stripes by changing the 
alpha values.

you should be able to take this and change it to what you are 
attempting. and yes, using Numeric arrays to do pixel operations like 
this will be incredibly faster than the set_at/get_at pixel poking 
functions :]




#!/usr/bin/env python
"""
simple test do play with a surface alpha, note that in
this example we use a 32bit display. we don't really need
a 32bit display, just anything besides 8bit. SDL doesn't
support blitting images with alpha onto 8bit surfaces.

we do need a 32bit image for the image when we are manipulating
the alpha values with surfarray.pixel_array
"""


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

#create a window
win = pygame.display.set_mode((300, 300), 0, 32)
win.fill((255,255,255), (0,0,150, 300))

#create a solid blue image
img = pygame.Surface((255, 255), SRCALPHA, 32)
img.fill((200, 200, 255))

#get buffer of  alpha for img
alpha = pygame.surfarray.pixels_alpha(img)

#set alpha as wavy lines
wavy = (sin(arange(255.0)*.5)+1)*128
alpha[:] = wavy.astype(UnsignedInt8)

#destroy buffer, which will "unlock" the img
del alpha #or could just do "alpha = None"

#let's see the results
win.blit(img, (22,22))
pygame.display.flip()

#wait till the user tires
while 1:
    e = pygame.event.wait()
    if e.type in (QUIT,MOUSEBUTTONDOWN,KEYDOWN): break