pythoncwindowsctypeswebcam

How to capture a webcam photo in Python using ctypes on windows?


I do not want to use OpenCV, Pygame or ImageIO because all three are HUGE, and I want to compile my program into a binary, using any of these libraries is causing the output to be 100MB or more when I am only using one functionality of those libraries.

I know that it is possible to capture a photo using c/++ but I do not know a single thing about low-level programming languages. So I was hoping for help.

NOTE: If anyone knows how to do this in C/++ but not ctypes, no problem, just post it here and I can modify it into python ctypes syntax.


Solution

  • I've published a lightweight Python camera library on PyPI: https://pypi.org/project/lite-camera/

    enter image description here

    To use it:

    import litecam
    
    camera = litecam.PyCamera()
    
    if camera.open(0):
    
        window = litecam.PyWindow(
            camera.getWidth(), camera.getHeight(), "Camera Stream")
    
        while window.waitKey('q'):
            frame = camera.captureFrame()
            if frame is not None:
                width = frame[0]
                height = frame[1]
                size = frame[2]
                data = frame[3]
                window.showFrame(width, height, data)
    
        camera.release()