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

[pygame] Bug: numpyarray.pixels_alpha array not properly aligned



Enclosed is a program that uses pixels_alpha to assign alpha to a surface. It displays two green rectangles, the top one using numpy, the bottom one using Numeric for reference. The center of each rectangle should have alpha 0, as is the case with the bottom rectangle, which appears as a green frame with the white background showing through. The numpy rectangle is scrambled. It would appear the array is not properly aligned with the surface's pitch. The rectangle displays correctly with image_size = (64, 64).

--
Lenard Lindstrom
<len-l@xxxxxxxxx>

# Shows bug with numpyarray.pixels_alpha.
# Displays two green rectangles with a transparent center.
# The top rectangle uses numpy. The bottom rectangle uses
# Numeric and is for reference.

import pygame as pg
import numpy as np
import Numeric as N
import cPickle

pg.init()

image_size = (100, 46)
image = pg.Surface(image_size, 0, 32)
image.fill((0, 255, 0, 255))

image_alpha_np = np.zeros(image_size, np.uint8)
image_alpha_np[...] = 255
image_alpha_np[10:-10, 10:-10] = 0

image_alpha_N = N.array(image_alpha_np, 'b')

window_size = (200, 200)

screen = pg.display.set_mode(window_size)
screen.fill((255, 255, 255, 255))

s = pg.Surface(image_size, pg.SRCALPHA, 32)
s.fill((255, 0, 0, 255))
s.blit(image, (0, 0))

# numpy version is on top
pg.numpyarray.pixels_alpha(s)[...] = image_alpha_np
screen.blit(s, ((window_size[0] - image_size[0]) // 2, 10))

# Numeric version is on bottom
pg.surfarray.pixels_alpha(s)[...] = image_alpha_N
screen.blit(s, ((window_size[0] - image_size[0]) // 2,
                window_size[1] - image_size[1] - 10))

pg.display.flip()

while 1:
    event = pg.event.wait()
    if event.type in [pg.KEYDOWN, pg.QUIT]:
        break