OpenCV cv2 image for PyGame image?

def cvimage_to_pygame(image):
    """Convert cvimage into a pygame image"""
    return pygame.image.frombuffer(image.tostring(), image.shape[:2],
                                   "RGB")

The function accepts a numpy array taken from cv2 camera. When I show the returned pyGame image in the pyGame window, it appears in three broken images. I do not know why this is so!

Any help would be greatly appreciated.

Here's what happens:

(Pygame on the left)

enter image description here

+4
source share
1 answer

In the parameters, the width and height of the field shapeare interchanged. Replace the argument:

image.shape[:2] # gives you (height, width) tuple

FROM

image.shape[1::-1] # gives you (width, height) tuple
+4
source

All Articles