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

Re: [pygame] Surfarray question



3d arrays do not preserve alpha. I was unaware that 2d arrays did. You can use array_alpha and pixels_alpha to manage the alpha plane separately.

src_array = pygame.surfarray.array3d(src_surface)
src_alpha = pygame.surfarray.array_alpha(src_surface)

.....

pygame.surfarray.blit_array(dst_surface, src_array)
pygame.surfarray.pixels_alpha(dst_array)[...] = src_alpha

This will copy both the color and alpha planes to dst_array. Then dst_array will do a normal alpha blit.

Lenard


Abhinav Lele wrote:
Thanks Lenard. Your solution solves the mystery of image not coming up during render, but there is small problem with setting the alpha to max value.

I have 2 image one of which is a jpg and second one a png file. The png file has transparency in certain regions. When I use array2d the transparency is preserved and i can see the bottom jpg file through the transparent regions of the png.
This would not be possible if i set alpha value to 255 for all pixels.

Is there a way out ?
From what I presume all this should be automatically done when I blit / copy whether i use array2d or array3d


-Abhinav

On Thu, Oct 23, 2008 at 1:38 AM, Lenard Lindstrom <len-l@xxxxxxxxx <mailto:len-l@xxxxxxxxx>> wrote:

    Hi Abhinav,

    You have found some unexpected behavior in pygame.surfarrray.
    blit_array is improperly named. It is a straight copy, not a true
    blit. And when a 3d array is copied to a 32 bit source alpha
    surface the per-pixel alpha is 0. The destination surface is
    completely transparent. The only workaround I can find is to set
    alpha to 255 after the blit_array() call:


          r_mysurface = storeObject.lib_get(o_id,"__render_surface")
          pygame.surfarray.blit_array(r_mysurface, r_myarray)
           pygame.surfarray.pixels_alpha(r_mysurface)[...] = 255


    In Pgame 1.9 this will also work:

           r_mysurface.fill((0,0,0,255), None, pgyame.BLIT_RGBA_MAX)


    I don't know if the 0 alpha is a bug or simply a peculiarity.
    Elsewhere in Pygame a 3-element color has an alpha of 255. But
    blit_array has probably set alpha to 0 since it was first
    implemented. It has simply gone unnoticed until now. Should it be
    changed?

    Lenard


    Abhinav Lele wrote:

        Hi,

        I am using surfarray to blit an image.
        When i use array2d i see the image being renderer, but with
        array3d i dont.


        I use the following code to load image :

                   r_temp_surface =
        pygame.image.load(fpath).convert_alpha()
                   salpha =  r_temp_surface.get_alpha()
                   r_surface = pygame.Surface((o_w,o_h)).convert_alpha()
pygame.transform.smoothscale(r_temp_surface,(o_w,o_h),r_surface)
                   #if salpha != None:
                   #    r_surface.set_alpha(salpha)
                   r_array = pygame.surfarray.array3d(r_surface)
                   print "New Surface Alpha: " , r_surface.get_alpha()
                   storeObject.lib_set(o_id,"__render_surface",r_surface)
                   storeObject.lib_set(o_id,"__render_array",r_array)



        To render I use :

               r_myarray = storeObject.lib_get(o_id,"__render_array")
               r_mysurface = storeObject.lib_get(o_id,"__render_surface")
               pygame.surfarray.blit_array(r_mysurface, r_myarray)
               surface.blit  (r_mysurface,(gx,gy))





--
Lenard Lindstrom
<len-l@xxxxxxxxx>