BoboTiG / python-mss

An ultra fast cross-platform multiple screenshots module in pure Python using ctypes.
https://pypi.org/project/mss/
MIT License
1.04k stars 94 forks source link

How to exclude mouse cursor from screenshot? #260

Closed kkew3 closed 1 year ago

kkew3 commented 1 year ago

General information:

For GNU/Linux users:

Description of the warning/error

The screenshot includes a mouse cursor even if with_cursor=False.

import pyautogui
import mss
with mss.mss(with_cursor=False) as sct:
    pyautogui.moveTo(100, 100)
    pyautogui.click()
    sct.shot(output='x.png')

Open x.png and there will be a mouse cursor at coordinate (100, 100).

Full message

No warning or error message.

Other details

BoboTiG commented 1 year ago

I just tried a simple script with only MSS, and no cursor is included. BTW the cursor is not included by default, you have to specifically ask for it using with_cursor=True.

kkew3 commented 1 year ago

On my computer, without using pyautogui, if I invoke the script via keyboard, once I type on the keyboard, e.g. typing "python ...", the mouse cursor disappears automatically, thus not included in mss's screenshot. Therefore, the use of pyautogui to force the appearance of mouse cursor is a must to reproduce the error.

I'm using a Mac, not GNU/Linux.

BoboTiG commented 1 year ago

Oh, sorry I read too quickly the description.

Currently with_cursor is only implemented on GNU/Linux. It seems that pyautogui is painting the cursor rather than moving it? And so it is part of the low-level image that MSS captures (simplified). Anyway, as MSS on macOS doen't know yet how to include the cursor, the issue should be raised on the pyautogui repository I guess.

BoboTiG commented 1 year ago

To see the cursor when you want to test, add a time.sleep(5) before mss.shot() so that you have time to move the cursor.

kkew3 commented 1 year ago

Thank you so much for your reply. I've found a workaround, which is to press Esc key, again using pyautogui, immediately before invoking mss, and the cursor is not included. Should I close the issue?

shininome commented 1 year ago
   def get_now_screen(self):
        monitor = {"top": 0, "left": 0, "width": 1075, "height": 900}
        with mss.mss() as sct:
            while True:
                pyautogui.press('esc',2,interval=0.1) 
                now_screen = np.array(sct.grab(monitor))
                # Put the captured screen in the queue
                self.queue.put(now_screen)

How did you solve the problem? I tried pressing 'Esc,' but it didn't work. The mouse pointer also appeared in the screenshot

kkew3 commented 1 year ago

My use case can be seen here, where I coded pyautogui.press('esc') only.