how to know if an image data is ABGR or RGBA from pygame?

When I load png files with pygame.image.load and then get data with pygame.surfarray.pixels2d, I get sometimes color data in ABGR format, and sometimes in RGBA, forcing me to a conversion if copy data from one type to another.

Problem is that I can't foresee if data will be ABGR or RGBA, and I comment or uncomment the conversion when I'm working on another file.

Is there a way to know data type from pygame ?

5

1 Answer

The pygame.surfarray module provides functions for separately referencing color channels and alpha channels of a pygame.Surface object in an array. Use these functions instead of pygame.surfarray.pixels2d():

pygame.surfarray.pixels3d():

Reference pixels into a 3d array

pygame.surfarray.pixels_alpha():

Reference pixel alphas into a 2d array

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like