Search code examples
pythonpython-imaging-librarypython-mss

convert mss screenshot to pil image


Im using mss to take a screenshot because it apparently can take fast screenshots. So is there a way to convert it into an image without saving it on my computer(its faster) like image.grab() in pillow. This is because i need to perform various actions like cropping finding the colour for which pillow is useful.

Traceback (most recent call last):
  File "/Users/Hari/Desktop/stuff/Tetris/test.py", line 84, in <module>
    img = im.crop((40*i+376, 40*j+485, 40*i+396, 40*j+505))
AttributeError: 'ScreenShot' object has no attribute 'crop'


with mss.mss() as sct:
    monitor = sct.monitors[1]
    im = sct.grab(monitor)

for i in range(0,10):
    for j in range(0, 18):
        img = im.crop((40*i+376, 40*j+485, 40*i+396, 40*j+505))
        rgb = img.convert('RGB')
        r, g, b = rgb.getpixel((1, 1))

Solution

  • The image can be converted to a PIL object, there's example code here.

    As you plan on cropping, consider doing that as part of the grabbing, instead of grabbing the whole screen and cropping as a post-processing.