pygame rotates camera stream from opencv camera

Viewed 106

I am trying to build a GUI using pygame but when I try to stream a camera using opencv and show it on the screen, pygame rotates the picture 90 degrees to the right. How do I prevent pygame from rotating the picture?

image = self.cam.read()  # getting the picture
if image is not None:
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # transforming the picture from BGR to RGB
    image = pygame.surfarray.make_surface(image)  # making the picture a pygame surface
    self.snapshot = image

# blit it to the display surface.  simple!
self.display.blit(self.snapshot, (0, 0))  # displaying the picture
pygame.display.update()  # updating the screen
1 Answers

Use numpy.transpose to permute the x and y axis of the image. This causes a rotation by 90°:

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = image.transpose(1, 0, 2)
Related