GithubHelp home page GithubHelp logo

bobotig / python-mss Goto Github PK

View Code? Open in Web Editor NEW
987.0 14.0 88.0 1.09 MB

An ultra fast cross-platform multiple screenshots module in pure Python using ctypes.

Home Page: https://pypi.org/project/mss/

License: MIT License

Python 99.66% Shell 0.34%
python screenshot ctypes python-library mss monitor cli efficiency gnu-linux macos

python-mss's Introduction

Python MSS

PyPI version Anaconda version Tests workflow Downloads

from mss import mss

# The simplest use, save a screen shot of the 1st monitor
with mss() as sct:
    sct.shot()

An ultra fast cross-platform multiple screenshots module in pure python using ctypes.

  • Python 3.8+, PEP8 compliant, no dependency, thread-safe;
  • very basic, it will grab one screen shot by monitor or a screen shot of all monitors and save it to a PNG file;
  • but you can use PIL and benefit from all its formats (or add yours directly);
  • integrate well with Numpy and OpenCV;
  • it could be easily embedded into games and other software which require fast and platform optimized methods to grab screen shots (like AI, Computer Vision);
  • get the source code on GitHub;
  • learn with a bunch of examples;
  • you can report a bug;
  • need some help? Use the tag python-mss on StackOverflow;
  • and there is a complete, and beautiful, documentation :)
  • MSS stands for Multiple Screen Shots;

Installation

You can install it with pip:

python -m pip install -U --user mss

Or you can install it with conda:

conda install -c conda-forge python-mss

python-mss's People

Contributors

andon-li avatar andreasbuhr avatar archangegabriel avatar bobotig avatar ctpahhuk-heba avatar cycomanic avatar dependabot[bot] avatar eruffaldi avatar foone avatar hugovk avatar karanlyons avatar mchlnix avatar mgorny avatar narumi147 avatar relent95 avatar ryanfox avatar sergeykalutsky avatar tonyl314 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-mss's Issues

mss.exception.ScreenShotError: gdi32.GetDIBits() failed.

General information:

  • OS name: Windows
  • OS version: 10
  • OS architecture: 64 bits
  • Resolutions:
    • Monitor 1: 1366 x 768
  • Python version: 3.6.1
  • MSS version: 3.3.2

Description of the warning/error

Hello, I have been trying to train a model using keras and getting input using mss by taking screenshots. Eventually, always roughly at the same time after 18 episodes or 2880 screenshots I always get this error around this time.

Full message

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Home\Programming\Python\My Programs\Games\2048 AI\2048 DQN.py", line 259, in <module>
    agent.run()
  File "C:\Users\user\Desktop\Home\Programming\Python\My Programs\Games\2048 AI\2048 DQN.py", line 197, in run
    next_state, reward, done = self.env.step(state, action)
  File "C:\Users\user\Desktop\Home\Programming\Python\My Programs\Games\2048 AI\2048 DQN.py", line 111, in step
    sct = self.window.screenshot()
  File "C:\Users\user\Desktop\Home\Programming\Python\My Programs\Games\2048 AI\2048 DQN.py", line 35, in screenshot
    return np.asarray(mss().grab(self.monitor))[:,:,:3]
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\mss\windows.py", line 223, in grab
    raise ScreenShotError("gdi32.GetDIBits() failed.")
mss.exception.ScreenShotError: gdi32.GetDIBits() failed.`

Other details

I understand this was an issue that was resolved in #59 however, I still appear to be getting it even after having the latest version of this module. If it helps I can share the full code for this project.

robustness to MSSWindows.get_pixels

shouldn't there by a try/except around the objects created and clean-up? otherwise if an exception happens between the two the objects will leak, and object leaks are not good on windows :)

'MSS' object has no attribute 'get_pixels'

General informations:

  • OS name: Windows
  • OS version: 10
  • OS architecture: 32 bits
  • Monitor(s)'s resolution: ?? (Two monitor)
  • Result of the command python --version: Python 2.7.14

Description of the warning/error

Full code:


import numpy as np
import cv2
from mss import mss
from PIL import Image

mon = {'top': 160, 'left': 160, 'width': 200, 'height': 200}

sct = mss()

while 1:
    sct.get_pixels(mon)
    img = Image.frombytes('RGB', (sct.width, sct.height), sct.image)
    cv2.imshow('test', np.array(img))
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

Full message

Traceback (most recent call last):
  File "C:\Users\Samuel\Desktop\scr2.py", line 11, in <module>
    sct.get_pixels(mon)
AttributeError: 'MSS' object has no attribute 'get_pixels'

c:\Users\Samuel\Desktop>

Value error on macbook

Hello,
I was trying to use your library on my macbook and I keep getting this error messages.

General informations:

  • OS name: macOS Sierra
  • OS version: 10.12.6 (16G29)
  • OS architecture: 64 bits
  • Result of the command python --version: Python 3.6.2

Description of the warning/error

When I use master branch I am able to do a few screenshots, that are getting smaller, and then I get the first error. It works the same way every time I restart python - first screenshot is good, another 3 are getting smaller and then I get an error.
When I switched to dev branch and I get immediately Value error.

Full message

  • Master branch
>>> from mss import mss
>>> with mss() as sct:
...     for x in range(10):
...             sct.shot()
... 
'monitor-1.png'
'monitor-1.png'
'monitor-1.png'
'monitor-1.png'
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/usr/local/lib/python3.6/site-packages/mss/base.py", line 131, in shot
    return next(self.save(**kwargs))
  File "/usr/local/lib/python3.6/site-packages/mss/base.py", line 121, in save
    to_png(sct.rgb, sct.size, output)
  File "/usr/local/lib/python3.6/site-packages/mss/base.py", line 255, in rgb
    self.raw[2::4], self.raw[1::4], self.raw[0::4]
ValueError: attempt to assign bytes of size 253362176 to extended slice of size 1327104000
  • Dev branch
>>> from mss import mss
>>> with mss() as sct:
...     sct.shot()
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/git/python-mss/mss/base.py", line 124, in shot
    return next(self.save(**kwargs))
  File "/git/python-mss/mss/base.py", line 114, in save
    to_png(sct.rgb, sct.size, output)
  File "/git/python-mss/mss/screenshot.py", line 133, in rgb
    self.raw[2::4], self.raw[1::4], self.raw[0::4]
ValueError: attempt to assign bytes of size 5184000 to extended slice of size 1296000

Other details

I tried also for filename in sct.save(): print(filename) but it crashed with same error messages.

MacOS X: get rid of the Quartz module

Use only ctypes, no more Quartz, aka big monster.

PS: I do not have an Apple machine, so it could take time. But it will be pushed for the version 2.0.0.

Hi-DPI not correctly detected in Windows 10

General informations:

  • OS name: Windows
  • OS version: 10, version 1709
  • OS architecture: 64 bits
  • Resolutions:
    • Monitor 1: 3840x2160
    • Monitor 2: 3840x2160
  • Python version: 3.6.1
  • MSS version: 3.2.0

Description of the warning/error

I have two monitors, both 3840x2160. My scaling is 200%. Screenshotting either one individually, or both together crops the right and bottom sides. I get two 2880x1620 images, or one 5760x1620.

monitors returns these values:

>>> for m in sct.monitors:
    print(m)

{'left': 0, 'top': 0, 'width': 5760, 'height': 1620, 'scale': 1.5}
{'left': 0, 'top': 0, 'width': 2880, 'height': 1620, 'scale': 1.5}
{'left': 2880, 'top': 0, 'width': 2880, 'height': 1620, 'scale': 1.5}

Other details

Dividing the resolution by 1.5 and multiplying by 2 gives the correct resolution - it seems to just be detecting the scaling incorrectly. I think this is related to #20.

Unreasonable memory usage on OS X

General informations:

  • OS name: Debian GNU/Linux
  • OS version: sid
  • OS architecture: 64 bits
  • Monitor(s)'s resolution:
  • Result of the command python --version: Python 3.5.1+

For GNU/Linux users:

  • Display server protocol and version, if known: X server
  • Desktop Environment: XFCE 4
  • Composite Window Manager name and version: Cairo v1.14.6

Description of the warning/error

Using mss (master branch or dev, both were tested) to take screenshots over any length of time on Mac results in a huge memory footprint and eventually the OS force-closing the program for exceeding the max usage.

Other details

Even at around 400 screenshots taken, my memory used is over 4 GB. This happens whether I instantiate mss() for each screenshot or re-use the same mss() object for all screenshots.

Can be reproduced by using the second OpenCV/NumPy example without the imshow()s and a longer runtime of 10+ seconds (at which point it's at the aforementioned 4 GB memory usage).

Mouse support

Any way to include mouse on screen grab?


Edit from @BoboTiG:

  • Linux support done with #232 in v8.0.0.
  • Mac support to be contributed.
  • Windows support done with #272 in vXXX.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

ImportError: cannot import name mss

General informations:

  • OS name: MacOS & Ubuntu
  • OS version: MacOS Sierra & Ubuntu 16.04
  • OS architecture: 64 bits
  • Result of the command python --version: Python 2.7.10 & Python 3.4.3

Surprised no one reported but I'm getting error:

ImportError: cannot import name mss

It happens on both MacOS and Ubuntu. I used:

pip install --upgrade mss

to install.

Any idea why?

shot() not working on Ubuntu through VNC

Hi there,
Thanks for your awesome library which is working like a charm on my local Mac.

However, I need to port it to ubuntu but when I run the shot() function I get the following error :
(It's is through VNC, running vnc4server)

X Error of failed request:  BadRequest (invalid request code or no such operation)
  Major opcode of failed request:  149 (RANDR)
  Minor opcode of failed request:  8 (RRGetScreenResources)
  Serial number of failed request:  11
  Current serial number in output stream:  11

Any clue about where it's coming from?

Thanks!

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

Optimize windows.py (3.2.0)

In 3.2.0 optimize:

+        if self._bbox != monitor:
+            self._bbox = monitor
+            self._bmi.bmiHeader.biWidth = width
+            self._bmi.bmiHeader.biHeight = -height  # Why minus? [1]
+            self._data = ctypes.create_string_buffer(width * height * 4)  # [2]
+            self._bmp = gdi.CreateCompatibleBitmap(self._srcdc, width, height)
+            gdi.SelectObject(self._memdc, self._bmp)

Maybe

        if self._bbox['width'] != width or self._bbox['height'] != height:
            self._bmi.bmiHeader.biWidth = width
            self._bmi.bmiHeader.biHeight = -height  # Why minus? [1]
            self._data = ctypes.create_string_buffer(width * height * 4)  # [2]
            self._bmp = gdi.CreateCompatibleBitmap(self._srcdc, width, height)
            gdi.SelectObject(self._memdc, self._bmp)
        self._bbox = monitor

Im get 1 pixel from different positions monitor or etc.

Grab screen with python multiprocessing up to 33FPS

General information:

  • OS name: Windows 10
  • OS architecture: 64 bits
  • Resolutions:
  • Python version: 3.6.7
  • MSS version: 3.2.0

So I am creating a project, where I am using TensorFlow object detection to detect enemies while playing CS:GO and shoot them. If someone is interested I wrote full step by step tutorial.

Anyway I am writing here because I am using MSS to grab a game screen. So at first I was getting ~20 FPS with grab screen, but when working on my project I decided to try improve this step, and I couldn't find better ways to grab screen in faster way, so I wrote a code while I am grabbing screen by using python multiprocessing. I divided whole script into two parts: first is to get a screen second to show a screen, these two parts are working in parallel processes so now I am receiving around 33 FPS. If someone is interested how I did this here is the link to my code.

Suggestion how I could make it work faster would be appreciated.

Use my example as much as you need !

Content of mss().monitors does not correspond with screen resolution, cannot take screenshots

General informations:

  • OS name: macOS
  • OS version: 10.13.1 "High Sierra"
  • OS architecture: 64 bits
  • Monitor(s)'s resolution: 2880x1800, default system scaling/resolution
  • Result of the command python --version: Python 2.7.10, however python3 --version yields Python 3.6.2 (which is what was used.)

Description of the warning/error

The content of mss().monitors is incorrect:

from mss import mss
mss().monitors

returns [{'left': 0, 'top': 0, 'width': 1680, 'height': 1050}, {'left': 0, 'top': 0, 'width': 1680, 'height': 1050}]

Full message

N/A

Other details

So far this is the only computer I've tested this on that has this issue, and it's the only hi-DPI computer I own.

SigSegv when using mss on latest ubuntu

General informations:

  • OS name: Ubuntu
  • OS version: 16.10
  • OS architecture: 64 bits
  • Result of the command python --version: Python 2.7.12+ and Python 3.5.2+

For GNU/Linux users:

  • Display server protocol and version, if known: X.Org X Server 1.18.4
  • Desktop Environment: N/A
  • Composite Window Manager name and version: qtile 10.6

Description of the warning/error

trying to use MSS results in a SIGSEGV when calling enum_display_monitors()
If I run the first lines of the example given in the README

>>> from mss import mss
>>> sct = mss(display=b':0')

# Retrieve monitors informations
>>> displays = sct.enum_display_monitors()

the program terminates with:
fish: โ€œpythonโ€ terminated by signal SIGSEGV (Address boundary error)

I've already tried recompiling the C library on my machine but no difference

Black image after screenshot: Windows Server 2016 , Python 3.6.0

Hello when am logged in to the remote desktop server the screenshot works fine but when i close the rdp all i get is a black image after screenshots. Why could it be so?Any suggestions on how to fix it?Thanks

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

Windows 8: screenshot not correct

On Windows 8.1, the screenshot does not appear as the screen looks. There is a mistake in same_img() or in get_pixels() at this line :

# Replace pixels values: BGR to RGB
self.image[2:buffer_len:3], self.image[0:buffer_len:3] = \
    self.image[0:buffer_len:3], self.image[2:buffer_len:3]

Without these lines, the screenshot is correct but RGB values are BGR.

Possible to save frames from videos with a high FPS?

from time import time
import mss
import mss.tools

start = time()
count = 1

while True:
	with mss.mss() as sct:
	    # The screen part to capture
	    monitor = {'top': 144, 'left': 80, 'width': 1397, 'height': 782}
	    output = 'frames/sct-{}.png'.format(count)
	    count += 1
	    # Grab the data
	    sct_img = sct.grab(monitor)

	    # Save to the picture file
	    mss.tools.to_png(sct_img.rgb, sct_img.size, output)
	    # print(output)
	    now = time()
	    if (now - start) % 60 >= 10: break

This seems to save only around 100 frames for 10 seconds. Can this be improved?

Example PIL

https://python-mss.readthedocs.io/en/dev/examples.html#pil
Os Windows

        # Create the Image, solution 1 (slower)
        # img = Image.frombytes('RGB', sct_img.size, sct_img.content)

sct_img.content - no attribute into sct_img

        # Create the Image, solution 2
        img = Image.frombytes('RGBA', sct_img.size, bytes(sct_img.raw), 'raw', 'BGRA')
        img = img.convert('RGB')  # Convert to RGB

Equivalent, which is faster
img = Image.frombytes('RGB', sct_img.size, sct_img.rgb, 'raw')

A high-DPI display isn't detected correctly in Windows 10

General informations:

  • OS name: Windows 10
  • OS version: 10.0.15063
  • OS architecture: 64 bits
  • Result of the platform.uname(): uname_result(system='Windows', node='DESKTOP-GTK56RF', release='10', version='10.0.15063', machine='AMD64', processor='Intel64 Family 6 Model 94 Stepping 3, GenuineIntel')
  • Result of the command python --version: Python 3.6.1 (MSC v.1900 32 bit (Intel)0)

Description of the warning/error

A high-DPI display isn't detected correctly in Windows 10.

Full message

Windows 10 allows scaling UI elements. My pc has the scaling size 125% and the resolution 1920x1080. But mmscan't detect the monitor with these settings correctly:

>>> from mss import mss
>>> sct = mss()
>>> sct.monitors
[{'height': 864, 'left': 0, 'top': 0, 'width': 1536},
 {'height': 864, 'left': 0, 'top': 0, 'width': 1536}]

When I change the scaling size to 100%, mms works fine.

libpng warning on linux

I have been using mss as the engine behind gui automation for linux in conjunction with visgrep and on some systems i get the following error.
libpng warning: Ignoring bad adaptive filter type
Then on systems with composite window managers, there is a bug as documented here:
https://bugs.launchpad.net/unity-2d/+bug/1081674

Is there a way to be able to capture the screen as i see it, without it rasterizing transparent layers that X windows seems to be introducing into the screenshot that is messing with the pattern matching.

Content of mss().monitors does not correspond with secondary screen resolution, cannot take screenshots (Windows 8.1)

General informations:

  • OS name: Windows
  • OS version: 8.1
  • OS architecture: 64 bits
  • Monitor(s)'s resolution: Monitor 1: 1600x900 ; Monitor 2: 1920x1080
  • Result of the command python --version: Python 2.7.9

Description of the warning/error

The content of mss().monitors is incorrect:

import mss
for monitor in mss.mss().monitors:
	print monitor

Return:

{'width': 2880, 'top': 0, 'height': 900, 'scale': 1.0, 'left': 0}
{'width': 1600, 'top': 0, 'height': 900, 'scale': 1.0, 'left': 0}
{'width': 1280, 'top': 0, 'height': 720, 'scale': 1.0, 'left': 1600}

The third line does not correspond with the secondary monitor resolution.

Full message

Here is a testing script I made:

import mss
import mss.tools
from Tkinter import *
app = Tk()

def screenshot():
	top = btn.winfo_rooty()
	left = btn.winfo_rootx()
	width = btn.winfo_width()
	height = btn.winfo_height()
	
	selection = {'top': top, 'left': left, 'width': width, 'height': height}
	sct_img = mss.mss().grab(selection)
	mss.tools.to_png(sct_img.rgb, sct_img.size, output="screenshot.png")


btn=Button(app, text="Grab a screenshot", command=screenshot)
btn.pack(fill=X,padx=50, pady=50)

print "Monitors:"
for monitor in mss.mss().monitors:
	print monitor

app.mainloop()

The script creates a simple Tkinter windows with a buton. A click on the buton save a screenshot of that button, using the coordinate provided by Tkinter.

This works when the windows is on the primary monitor, but does not work after I drag the windows on the secondaty monitor: the screenshot represents another part of the monitor.

Other details

When I set the secondary monitor resolution to correspond with the resolution provided by mss, then the screenshot works perfectly on both monitors.

The script works just fine on Ubuntu.

Grab a screenshot from a virtual Desktop on Windows

General informations:

  • OS name: Windows 10
  • OS version: 1803
  • OS architecture: 64 bits
  • Resolutions:
    • Monitor 1: 1680x1050
  • Python version: 3.6.7
  • MSS version: 3.3.1

Description of the warning/error

Grab screen from secondary desktop on monitor.

Full message

My problem is, that I do not have second monitor but second Desktop in Windows 10 (when you click on Task View icon, you can add New Desktop and place some programs there). So I want to grab and save screenshot from that desktop even when I am working in desktop1. Any idea how to do it there please?

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

PUBG Fullscreen doesn't capture as expected

  • OS name: Windows
  • OS version: 8
  • OS architecture: 64 bits
  • Result of the command python --version: Python 3.6.2

Description of the warning/error

  • Run PLAYERUNKNOWN'S BATTLEGROUNDS game (PUBG) in Fullscreen mode.
  • Try to capture monitor output

Actual output

  • Output shows whatever is displayed behind game.

Expected output

  • Output will show game.

Other details

Add a shortcut to take automatically use the proper MSS class

Something like:

from platform import system

def mss(*args, **kwargs):
    """ Factory returning a proper MSS class instance 

        It detects the plateform we are running on 
        and choose the most adapted mss_class to take
        screenshots.

        It then proxies its arguments to the class for 
        instanciation.
    """

    mss_class = {
        'Darwin': MSSMac,
        'Linux': MSSLinux,
        'Windows': MSSWindows
    }[system()]

    return mss_class(*args, **kwargs)

So so mss can be used as simply as:

from mss import mss

screenshotter = mss()
for f in screenshotter.save():
    print(f)

This avoid to write the same boiler plate over and over fow the most common use case.

AttributeError: _data / "gdi32.GetDIBits() failed" (Windows)

General information:

  • OS name: Windows
  • OS version: 10, version 1803
  • OS architecture: 64-bit
  • Resolutions:
    • Monitor 1: 1920x1080
    • Monitor 2: 1920x1080
  • Python version: 3.6.5
  • MSS version: 3.2.1

Description of the warning/error

MSS consistently throws the following error, always after my program has been running for a little over 3 hours, and after successfully capturing many (22,000+) screenshots in that time.

Full message

== 2018-07-15 07:17:17.953381 runtime: 3:09:39.428722 ==
('gdi32.GetDIBits() failed.', {'bits': 0, 'height': 665, 'width': 1033, 'gdi': <WinDLL 'gdi32', handle 7ffeff9b0000 at 0x1e9c1896898>, 'monitor': {'left': -1034, 'top': 31, 'width': 1033, 'height': 665}, 'self': <mss.windows.MSS object at 0x000001E9C1896860>})
Traceback (most recent call last):
  File "D:\IdeaProjects\Python\CaptureTest\main.py", line 98, in <module>
    run()
  File "D:\IdeaProjects\Python\CaptureTest\main.py", line 82, in run
    self.screenshot()
  File "D:\IdeaProjects\Python\CaptureTest\main.py", line 77, in screenshot
    self.im = self.window.screenshot()
  File "D:\IdeaProjects\Python\CaptureTest\main.py", line 84, in screenshot
    im = self.mss_instance.grab(area)
  File "C:\Users\Nick\AppData\Local\Programs\Python\Python36\lib\site-packages\mss\windows.py", line 202, in grab
    del self._data
AttributeError: _data

Other details

I've tried to replicate this error by simply calling mss.grab(...) in a while loop and seeing if it breaks after x number of screenshots, but that runs fine for at least 160,000 iterations, so I can't figure out why my program consistently seems to break at ~3 hours runtime.

Upon viewing the MSS source, I realised that the del self._data line is directly followed by raise ScreenShotError('gdi32.GetDIBits() failed.', locals()), so I suppose my question is:

What could cause gdi32.GetDIBits() to spontaneously fail?

Screen not being captured correctly

Hi
I've been trying to find a way to capture my Mac's screen to use to train a neural network, but I couldn't find a way to capture the screen with decent FPS until I found your library. The FPS using this library are very good, however the screen is not being captured correctly.

General informations:

  • OS name: macOS
  • OS version: 10.12.5
  • OS architecture: 64 bits
  • Result of the command python --version: Python 3.6.0 :: Anaconda custom (x86_64)

For GNU/Linux users:

  • Desktop Environment: Quartz

Description of the warning/error

Using grab() to capture either the whole screen or a section of the screen, the display is not captured properly, looking like every other row of pixels has been shifted to the side. This happens when using OpenCV to show the image and when saving the image to a file.

screenshot
screen shot 2017-08-15 at 00 38 59

Code (OpenCV/Numpy Example Code):

import time

import cv2
import mss
import numpy


with mss.mss() as sct:
    # Part of the screen to capture
    monitor = {'top': 50, 'left': 0, 'width': 840, 'height': 940}

    while 'Screen capturing':
        last_time = time.time()

        # Get raw pixels from the screen, save it to a Numpy array
        img = numpy.array(sct.grab(monitor))

        # Display the picture
        # cv2.resize(img, (40, 40))
        cv2.imshow('OpenCV/Numpy normal', img)

        # Display the picture in grayscale
        # cv2.imshow('OpenCV/Numpy grayscale',
       # cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY))

        print('fps: {0}'.format(1 / (time.time()-last_time)))

        # Press "q" to quit
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

I am using the dev branch as the master branch does not work on Macs.

Full message

N/A

Other details

This happens when capturing the whole screen and a section of the screen.

Thank You.

High FPS Greyscale Screenshots

Hi.

I'm interested in capturing greyscale screenshots at a very high rate for use in machine learning tasks (see [1] for an example). I've found mss to be fast at capturing color and greyscale screenshots compared to other solutions. Nevertheless, on MacOS, to use cv2 with them via PIL, I think the BGR<->RGB happens twice (once in mss and another time in PIL) before I send it off to greyscale.

This made me think to ask you if an option to go straight to greyscale would make sense for mss. I saw an old issue closed stating that greyscale wasn't in line with project goals. I suspect that would be because someone can convert easily to greyscale down the line. However, if the conversion happens upstream at mss, the data flowing downstream is 1/3 the size I believe. Perhaps there would be an frames per second (performance) boost if greyscale could be captured early/directly?

Thanks for this great project.

Please let me know your thoughts. (I'm new to graphical processing in python3.)

Thanks,
Joshua

[1] https://www.youtube.com/watch?v=edWI4ZnWUGg

Example code seems to be wrong

https://python-mss.readthedocs.io/en/dev/examples.html#playing-with-pixels

In this example, sct.monitor is passed directly to grab method.
In osx, grab changes a value of monitor param. As a result of it, the value of sct.monitor is also changed because python passes reference of dict rather than copying it.
(https://github.com/BoboTiG/python-mss/blob/master/mss/darwin.py#L182)

I think it's better to copy the dict before passing it to grab.

sct_img = sct.grab(sct.monitors[1].copy())

Massive memory usage when using MSS with queues

General informations:

  • OS name: macOS
  • OS version: 10.12.5 "Sierra"
  • OS architecture: 64 bits
  • Result of the command python --version: Python 2.7.10, however python3 --version yields Python 3.6.2 (which is what was used.)

For GNU/Linux users:

  • Desktop Environment: Quartz

Description of the warning/error

Putting and removing images captured using MSS in a queue results in a massive memory usage. This example code resulted in about 6GB used in less that 15 seconds.

from mss import mss
import time
import queue

myqueue = queue.Queue()

while True:
    with mss() as sct:
        myqueue.put(sct.shot())
    time.sleep(0.25)
    print(myqueue.get())

Full message

N/A

Other details

Willing to provide more info as needed!

Better tests

Need a lot more tests, any help appreciated :)

Running OpenCV/Numpy example makes window larger and larger until crash

General informations:

  • OS name: macOS
  • OS version: 10.12.5
  • OS architecture: 64 bits
  • Result of the command python --version: Python 2.7.10

Description of the warning/error

When running this code, the output window will gradually increase in size until the program crashes.

Full message

(cv) Guests-MBP:opencv admin$ python scratch.py
sh: sysctl: command not found
fps: 14.7629580092
fps: 49.8900215294
fps: 36.1858683461
fps: 12.818855861
fps: 2.33973450345
fps: 0.709336639047
fps: 0.214847465514
Traceback (most recent call last):
  File "scratch.py", line 16, in <module>
    img = numpy.array(sct.grab(monitor))
  File "/Users/admin/.virtualenvs/cv/lib/python2.7/site-packages/mss/darwin.py", line 168, in grab
    'CoreGraphics.CGWindowListCreateImage() failed.', locals())
mss.exception.ScreenShotError: ('CoreGraphics.CGWindowListCreateImage() failed.', {'self': <mss.darwin.MSS object at 0x10aa80e10>, 'image_ref': None, 'monitor': {'width': 12288, 'top': 40, 'height': 12800, 'left': 40}, 'rect': <mss.darwin.CGRect object at 0x10aa834d0>, 'rounded_width': 12288.0})

Other details

A (rather large) GIF of the issue:

56cb8a2d30e92267df9406a65db1c7a8

filename's dir is not used when saving

You do
{code}
dir_ = os.getcwd()
fname = os.path.join(dir_, os.path.basename(fname))
{code}
instead of using the dir from output/fname directly. Is that made for purpose or not?
As a user i want to control the dir I save shots to.

Black screens in games like League of Legends

General informations:

  • OS name: Debian GNU/Linux
  • OS version: sid
  • OS architecture: 64 bits
  • Resolutions:
    • Monitor 1: 800x600
    • Monitor 2: 1920x1080
  • Python version: 3.6.4
  • MSS version: 3.2.0

For GNU/Linux users:

  • Display server protocol and version, if known: X server
  • Desktop Environment: XFCE 4
  • Composite Window Manager name and version: Cairo v1.14.6

Description of the warning/error

Black screens in games that use directX

Full message

The image is totally black. How much effort do you think is necessary to support directX full-screen captures? Looking at OBS (https://github.com/obsproject/obs-studio/blob/master/plugins/win-capture/game-capture.c) it needs like 2,000 lines of code. I thought on do it by myself but i'm new to C and still don't know if its possible to make such module for Python! Can you help me or give me hints on where to start (Or tell me if it is possible to do it in the first place!?)?

Mss screen grab makes screen responsiveness laggy (while gaming/moving a windows explorer)

General informations:

What i have done? Own implementation diy ambilight using mss screengrab

  • OS name: Windows
  • OS version: 10
  • OS architecture: 64 bits
  • Resolutions (tested):
    • Monitor 1: 1080p@120Hz
  • Python version: _2.7
  • MSS version: last from pip
  • CPU: i7 4790k
  • GPU: GTX 1080

Description of the warning/error

Threaded screen capture is very laggy while gaming

Full message

When i create a threaded class to capture the screen as fast as possible, it is makes my screen resposiveness very laggy... I can not play well. After making a sleep : Time.sleep(0.5), it does not slow down gaming, but reaction time for leds is too slow...

Pseudo code:

class Ambilight(Thread):
def __init__(self, LightController):
  self.screen = mss()
  self.imageCaptured = None
  self.LightController = LightController

# ...

def run(self):
 while True:
    self.imageCaptured = np.array(self.screen.grab( self.screen.monitors[0]))
    self.LightController.ImageUpdate( self.imageCaptured.copy() )

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

Fullscreen doesn't capture as expected with fullscreen game

Hi,

I was under the impression that this package should be able to make game screenshot.
I did the following :

import time

#launch script then alt+tab to game
n_seconds = 5
for x in range(n_seconds):
	print "taking screenshot in {}".format(n_seconds-x)
	time.sleep(1)

from mss.windows import MSS as mss
sct = mss()
filename = sct.shot(output='test.png')

However all I'm getting is this :

https://www.dropbox.com/s/yy5qnltobdp02v6/test.png?dl=0

Is this what is expected?
Thanks.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

Bad multiple screen grab

I'm working on 10.12.1 MacOS Sierra with 2.0.18 mss version and python 3.5. When today I tried to create multiple screenshots using mss according to documentation.
And with two different screens with resolutions
{'height': 1200, 'left': 0, 'top': 0, 'width': 1920}, {'height': 768, 'left': -1366, 'top': 0, 'width': 1366}. The bigger screenshot looks fine but the smaller gives a very bad result: http://imgur.com/a/hKrHw. What is the cause of it?

Screen reconfiguration not handled correctly

General informations:

  • OS name: Linux Mint
  • OS version: xenial
  • OS architecture: 64 bit
  • Resolutions:
    • Monitor 1: 1920x1080
    • Monitor 2: 1920x1080
    • Monitor 3: 1920x1080
  • Python version: 2.7.12
  • MSS version: 3.3.1

Description of the warning/error

  1. start a python script while only one monitor connected
  2. take a screenshot
  3. connect more monitors
  4. take a screenshot of monitor "-1"
  5. only the monitor attached when taking the first screenshot is screenshoted.

Full message

no message

Other details

The reason is that the "_monitors" is a class varable of MSSBase

_monitors = [] # type: List[Dict[str, int]]

and it is only initialized once per script run:

if not self._monitors:

One possible solution is to turn "_monitors" into a member variable.

Feature: copy to clipboard

A proposal for a new feature: copy the screenshot content into the clipboard.

Without needed to handle ourselve the clipboard stuff, just using pyperclip.

The feature can be as simple as adding a new CLI argument like -cb|--clipboard into __main__.py.

I do not think it is a good thing to integrate the feature inside MSS because if someone wanted to add such thing into another application, it would just have to get raw pixels from a screenshot, which is already possible easily.

Note: idea taken from this SO question.

OpenCV/Numpy example not working correctly on macOS

Hi,
First of all, thank for a great library.
I encountered some issues while trying to record my screen.

General informations:

  • MSS version: 3.0.1
  • OS name: macOS Sierra
  • OS version: 10.12.5 (16F73)
  • OS architecture: 64 bits
  • Result of the command python --version: Python 3.5.3
  • OpenCV version: opencv3 : 3.1.0
  • Numpy Version: 1.12.1

Description of the warning/error

I tried out OpenCV/Numpy example to record the screen.
But the frames are getting delayed with time(exponentially), I think there are lots of GC happening.
I interrupted the command line after fps: 0.07140145917730202 because the display window was not responding.
Ways to reproduce: It is happening every time.

Full message

Rohits-MacBook-Pro:drive rohitarya$ python3 readscreen.py 
fps: 4.144634697781783
fps: 1.2977357482958785
fps: 0.3543577094166482
fps: 0.07140145917730202
^Cpython3(19339,0x7fffe9aae3c0) malloc: *** mach_vm_map(size=8404176896) failed (error code=4)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
python3(19339,0x7fffe9aae3c0) malloc: *** mach_vm_map(size=8404176896) failed (error code=4)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
python3(19339,0x7fffe9aae3c0) malloc: *** mach_vm_map(size=8404176896) failed (error code=4)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
python3(19339,0x7fffe9aae3c0) malloc: *** mach_vm_map(size=8404176896) failed (error code=4)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
python3(19339,0x7fffe9aae3c0) malloc: *** mach_vm_map(size=8404176896) failed (error code=4)
*** error: can't allocate region

Other Information

Rest of the examples(Part of the screen, PIL, Playing with pixels) are working perfectly fine.

[XServer] Library makes program hard crash when monitor is out of bounds

General information:

  • OS name: Ubuntu (ubuntu-desktop)
  • OS version: 18.04 LTS
  • OS architecture: 64 bits
  • Resolutions:
    • Monitor 1: 1920x1080 (laptop built-in)
    • Monitor 2: 1366x768 (external)
  • Python version: 3.6.5
  • MSS version: 3.2.0

For GNU/Linux users:

  • Display server protocol and version, if known: X server (X.Org version: 1.19.6)
  • Desktop Environment: Gnome 3
  • Composite Window Manager name and version: ??? (The default one, I guess?)

Description of the warning/error

This error occurs when the a monitor that is out of bounds is specified in the grab() call.

from mss import mss

screenshotter = mss()
monitor = {
    "left": -30,
    "top": 0,
    "width": 100,
    "height": 100,
}
try:
    screenshotter.grab(monitor)  # X Error
except BaseException as e:
    # Never reached
    print("Handling error")

Using a monitor like {"left": 0, "top": 0, "width": 10000, "height": 100} has the same result.

Full message

X Error of failed request:  BadMatch (invalid parameter attributes)
  Major opcode of failed request:  73 (X_GetImage)
  Serial number of failed request:  91
  Current serial number in output stream:  91

The message is printed to stderr, then the Python interpreter quits with exit code 1.

Other details

I discovered this because I let my program generate a box based on the location of the cursor. Then I take the screenshot of only the cursor area.

While it might be better for efficiency not to check the bounds of the monitor, I personally don't think causing this type of hard crash is acceptable. It's up to you. If you decide not to check bounds, I think a mention of this in the documentation would be appropriate.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.