Convert image from pygame to PIL image

后端 未结 1 1921
生来不讨喜
生来不讨喜 2020-12-21 02:03

we are using a Raspberry Pi + Python 3.4 + PyGame to capture an image from a specific USB webcam. We use this simple code to capture (it works ok):

相关标签:
1条回答
  • 2020-12-21 02:50

    As per Damian Yerrick's comment, under Python 3 the result of pygame.image.tostring() is a bytes, despite the method's name. Thus we can go out of this situation with this simple code:

    pygame.camera.init()
    cam = pygame.camera.Camera(pygame.camera.list_cameras()[0],(1280,720))
    cam.start()
    time.sleep(1)
    webcamImage = cam.get_image()
    pil_string_image = pygame.image.tostring(webcamImage,"RGBA",False)
    im = Image.frombytes("RGBA",(1280,720),pil_string_image)
    
    0 讨论(0)
提交回复
热议问题