0

I am using pygame to create my own GUI and which shows the camera preview on LCD. Meantime I want to save the camera recording in the file.

For this reason I capture the stream in JPEG format, than use numpy read into buffer in JPEG format and than using cv2.decode decode it to image. After that I am able to write it as a video.

But pygame module cannot accept numpy object, it needs surface element. And I don’t know how to convert the imageData to surface object, for pygame.image.fromstring can use it.

Please help to provide JPEG image to pygame.image.fromstring object.

pygame.init()
screen = pygame.display.set_mode((LCD_W,LCD_H))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output2.avi',fourcc, 30.0, (640,480))

while (True):

    camera.capture(stream, use_video_port=True, format='jpeg')              
    stream.seek(0)

    imageData = np.fromstring(stream.getvalue(), dtype=np.uint8)
    npImage = cv2.imdecode(imageData, 1) 
    out.write(npImage)

    screen.blit(npImage,             # gives error on npImage type
      ((LCD_W - img.get_width() ) / 2,
      (LCD_H - img.get_height()) / 2))


    img = pygame.image.fromstring(               # gives ValueError saying cannot read 
    stream.getvalue(),
      sizeData[sizeMode][1], 'RGB') 

    stream.close()
haykp
  • 435
  • 12
  • 29
  • Can you please provide the text of the `ValueError` exception? – nyr1o Feb 28 '18 at 13:59
  • And what's the type of `stream`, since it is not initialized in this piece of code? – nyr1o Feb 28 '18 at 14:03
  • stream = io.BytesIO() , it says that for pygame and errors says 'pygame.Surface' object is needed – haykp Feb 28 '18 at 16:00
  • >>> sizeData[sizeMode][1], 'RGB') >>ValueError: String length does not equal format and resolution size I think the reason is that I wrote 'RGB' as type – haykp Feb 28 '18 at 16:04

0 Answers0