def cvimage_to_pygame(image):
"""Convert cvimage into a pygame image"""
return pygame.image.frombuffer(image.tostring(), image.shape[:2],
"RGB")
The function takes a numpy array taken from the cv2 camera. When I display the returned pyGame image on a pyGame window, it appears in three broken images. I don't know why this is!
Any help would be greatly appreciated.
Heres what happens::
(Pygame on the left)
Step 1: First, import the libraries Image and Pygame. Step 2: Now, take the colors as input that you want to use in the game. Step 3: Then, construct the GUI game. Step 4: Further, set the dimensions of your GUI game.
The drawing functions are fine if you want to draw simple shapes on the screen, but many games have images (also called sprites). Pygame is able to load images onto Surface objects from PNG, JPG, GIF, and BMP image files.
pygame.Surface.convert. — change the pixel format of an image.
In the shape
field width and height parameters are swapped. Replace argument:
image.shape[:2] # gives you (height, width) tuple
With
image.shape[1::-1] # gives you (width, height) tuple
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With