Zack Schilling wrote:
In Pygame 1.8.1 and the latest 1.9.0 unstable, I've been having
trouble using the surfarray module's pixels_alpha() and array_alpha()
functions. I'm using 32bpp images with per-pixel alpha and I'm trying
to work with the alpha data in numpy.
At first I couldn't get surfarray.array_alpha() to return a proper
array (it returned all opaque values, surfarray.pixels_alpha() worked
fine).
[snip]
So I went to find out why array_alpha wasn't working properly. I
investigated in the pygame source and found the culprit in
"pygame/_numpysurfarray.py"
The following code, intended to catch 8 bit images or images without
alpha values, was erroneously catching my perpixel images.
if surface.get_bytesize () == 1 or not surface.get_alpha ():
# 1 bpp surfaces and surfaces without alpha are always fully
# opaque.
The problem was that per-pixel surfaces normally have the blacket
alpha, returned by get_alpha(), set as 255, a True value. Surfaces
returned by convert_alpha() have blanket alpha 0, a False value. This
0 value caused the failure.
The fix is simple, make sure that the image is less than 32bpp AND
missing an alpha channel before giving up on it.
if surface.get_bytesize () == 1 or (not surface.get_alpha () and
surface.get_bytesize () < 4):
# 1 bpp surfaces and surfaces without alpha are always fully
# opaque.
A pygame developer should make and commit that change.
Fixed in SVN rev. 1903. As long as the blanket alpha is not None, its
value shouldn't matter for a per-pixel alpha surface as it is ignored.
But a surface with a blanket alpha set to None is totally opaque, any
per-pixel alpha values are ignored. Also 2 byte surfaces can have
per-pixel alpha. So all there factors had to be considered when
patching _numpysurfarray.py.
Once I fixed that issue, everything worked as expected... as long as
I ran the script from the command line. Now if I used either
array_alpha() or pixels_alpha() in a py2exe/p2app program, I got a
segmentation fault! ( just like before the change with pixels_alpha() )
This is next on the agenda.