Question

I am trying to apply a numpy array of uint8 alpha values (pickled array here) to a surface, but I keep getting a ValueError: unsupported colormasks for alpha reference array.

s = pygame.Surface((100, 100))
s.fill((126, 126, 126))  # make it grey
pxa = pg.surfarray.pixels_alpha(s)

Here is the full traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-81-8c19259f8aa5> in <module>()
----> 1 pxa = pg.surfarray.pixels_alpha(s)

/usr/lib/python2.7/dist-packages/pygame/surfarray.pyc in pixels_alpha(surface)
    206         return numericsf.pixels_alpha (surface)
    207     elif __arraytype == "numpy":
--> 208         return numpysf.pixels_alpha (surface)
    209     raise NotImplementedError("surface arrays are not supported")
    210 

/usr/lib/python2.7/dist-packages/pygame/_numpysurfarray.pyc in pixels_alpha(surface)
    295         start = 3
    296     else:
--> 297         raise ValueError("unsupported colormasks for alpha reference array")
    298 
    299     array = numpy.ndarray \

ValueError: unsupported colormasks for alpha reference array

The pixels_alpha docstring mentions that a 32-bit Surface with per-pixel alpha values are required. I checked the surfaces bit size with Surface.get_bitsize, and confirmed that it returns 32. That being said, I have no idea how to check if it has per-pixel alpha values. Might this be the problem? If so, how do I check? If not, what's am I missing?

Thank you!

Was it helpful?

Solution

You should initialise with per-pixel alpha:

surface = pygame.Surface((100, 100), flags=pygame.SRCALPHA)

This does not require an initialised display.


Old answer:

You need it to have per-pixel alpha, which isn't automatic even for 32-bit images:

surface = pygame.Surface((100, 100)).convert_alpha()

And to use convert_alpha you need to have initialised the display:

pygame.display.set_mode((X, Y))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top