python-mss

Can I get the screenshot data as a file-like object or the image in bytes without saving any file?


I want to send a screenshot over a discord webhook. I am using the dhooks module for this. To send a file, I need a "file-like object" so I want to get a file-like object without having the need to store the screenshot as a file. Any idea how to do this?


Solution

  • I just added an example in the documentation:

    import mss
    import mss.tools
    
    
    with mss.mss() as sct:
        # The monitor or screen part to capture
        monitor = sct.monitors[1]  # or a region
    
        # Grab the data
        sct_img = sct.grab(monitor)
    
        # Generate the PNG
        png = mss.tools.to_png(sct_img.rgb, sct_img.size)