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

[pygame] converting byte image for display



Hello,
I've just started learning and understanding pygame. This my first
post to the list.

I'm stuck in optimizing a display loop for 8bit image buffers.

As far as I understand the image has to be in RGB format to be
displayed. I tried,but couldn't get the image.frombuffer(..mode="P")
working. Psyco is in use and it helps, but the loop is still too slow.

Is there faster way to convert a byte buffer to 3-byte RGB buffer than
the function below?
How to display the byte buffer without modifications to it?

Any ideas, suggestions?

Thanks,
-- HannuK

___________________clip_____________________

def to_bw(rawbuffer):
    retstr=''
    for e in rawbuffer :
        retstr = retstr + e*3
    return retstr

pygame.init()
screen = pygame.display.set_mode((width,height),
DOUBLEBUF|HWSURFACE|HWPALETTE,8)
screen = pygame.display.get_surface()
#pygame.display.set_palette([(x,x,x) for x in range(256)])

while True:
    (width,height,framebuffer) = getbuf()
    bwbuf = to_bw(framebuffer)

    bwimage = pygame.image.frombuffer(bwbuf, (width,height), "RGB")
    screen.blit(bwimage, (0,0))
    pygame.display.flip()
    for event in pygame.event.get():
      if event.type == QUIT:
         sys.exit(0)