3

Can you please help me understand why pygame shows my image as mirrored? Please see the attached image for more info:

enter image description here

I am capturing an image from PiCamera into PiRGBArray stream, then creating a pygame surface module and displaying the image.

This is the code:

camera = PiCamera()
camera.resolution = (640, 480)
rawCapture = PiRGBArray(camera, size=(640, 480))

pygame.init()
lcdDisplay = pygame.display.set_mode((480,640))

for frame in camera.capture_continuous(rawCapture, format="rgb", 
    image = frame.array

    surf=pygame.surfarray.make_surface(image)
    lcdDisplay.blit(surf, (0,0))    

    pygame.display.update()
    pygame.display.flip()

    # clear the stream 
    rawCapture.truncate(0)
caffreyd
  • 1,151
  • 1
  • 17
  • 25
haykp
  • 435
  • 12
  • 29
  • you call: `pygame.display.flip()`. Would that have something to do with it? – Paul H Mar 04 '18 at 03:14
  • Doesn't look like it: https://www.pygame.org/docs/ref/display.html#pygame.display.flip – whackamadoodle3000 Mar 04 '18 at 03:20
  • the display.flip does not do anything, I was hoping that it may help but no – haykp Mar 04 '18 at 04:15
  • When I show the image by opencv cv2.imshow than the image is normal – haykp Mar 04 '18 at 04:25
  • `pygame.display.flip()` just updates the window similar to `pygame.display.update()`. If you want to flip the image/surface in pygame, you can use [`pygame.transform.flip`](http://www.pygame.org/docs/ref/transform.html#pygame.transform.flip). – skrx Mar 04 '18 at 09:25
  • yeap pygam.transform.flip makes flip, but still the letters are shown mirrored in the picuture(( – haykp Mar 04 '18 at 09:53

2 Answers2

1

You're capturing the image from a camera, and a camera is not a mirror :-)

Think of it this way: if the camera is facing front, and you take a picture, it'll look "correct". If you flip it to face it towards you, the image is flipped.

This is not related to pygame, it's just how cameras (webcams) work.

ChatterOne
  • 3,381
  • 1
  • 18
  • 24
  • Can you please explain more, not sure that I got your point. Just pay attention to the fact that cv2.imshow on the same image works fine – haykp Mar 04 '18 at 10:32
  • Unfortunately I'm not that good at explaining, but if you google a bit you'll find lots of links, for example http://www.rawinfopages.com/tips/2014/04/choose-normal-or-flipped-webcam-images/ . – ChatterOne Mar 05 '18 at 08:14
1

I don't have experience on this but as far as I understand from documentation there are ways to control your camera to flip the video or not, here is comments from tutorial :

Using Camera Controls

Most cameras support controls like flipping the image and changing brightness. set_controls() and get_controls() can be used at any point after using start().

cam.set_controls(hflip = True, vflip = False)

Hope this is your case.

The other strange thing I noticed in your code is when you define camera you use resolution (640, 480), but when defining the display yo use (480, 640), is this intentional or just and error ?

Also there is similar question/answer which might help you more. It has link to nice blog on same topic.

Gagik Sukiasyan
  • 846
  • 6
  • 17
  • pygame takes (w,h) but camera takes (h,w) thus the resolution difference. The camera controlling stuff just tried, doesnt help(( – haykp Mar 05 '18 at 03:50