python-mss icon indicating copy to clipboard operation
python-mss copied to clipboard

Mouse support

Open itsdax opened this issue 7 years ago • 19 comments
trafficstars

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.

itsdax avatar Apr 09 '18 10:04 itsdax

As of now, no. But it may be an interesting feature to add.

BoboTiG avatar Apr 09 '18 11:04 BoboTiG

As an interim solution, could you capture the mouse position on screen and add it to the .png file metadata on write out?

A good example of getting the mouse position is given in the 2nd answer here: https://stackoverflow.com/questions/3698635/getting-cursor-position-in-python

jamespreed avatar May 26 '18 02:05 jamespreed

It could be a good start.

Perhaps adding CLI arguments like --with-cursor and --cursor-file=FILE. Add adding keywords/attributes with_cursor=False and cursor_file='' to the MSS class.

I think it can be interesting to merge the cursor into the pixels directly and not only in the final PNG.

What do ou think @itsdax and @jamespreed ?

BoboTiG avatar May 26 '18 12:05 BoboTiG

Yes! That sounds like the right approach.

In Java, there's a method for getting the cursor's state (text hover, resize, move, etc) java.awt.Cursor.getType()

If there's a similar one in python, it can be used to trigger different icons.

itsdax avatar May 26 '18 19:05 itsdax

Integrating a cursor directly into the pixels would be great. You could have several choices: cursor, crosshair, or concentric circles.

jamespreed avatar May 28 '18 01:05 jamespreed

FI we can find some clues from the pyinput module.

BoboTiG avatar Aug 07 '18 08:08 BoboTiG

This is how we could do it for GNU/Linux: https://github.com/MaartenBaert/ssr/blob/master/src/AV/Input/X11Input.cpp

BoboTiG avatar Jan 29 '19 21:01 BoboTiG

I've written a solution for Windows.

The script gets the current mouse cursor icon to bitmap and adds it to the screenshot. It's very fast, so no performance problems.

However, I'm not quite sure how to implement it. The dependencies to win32gui can be exchanged for call to cTypes for sure. Maybe someone else can add the code in a beautiful fashion?

import numpy as np
import win32gui, win32ui
import mss
from PIL import Image

def set_pixel(img, w, x, y, rgb=(0,0,0)):
    """
    Set a pixel in a, RGB byte array
    """
    pos = (x*w + y)*3
    if pos>=len(img):return img # avoid setting pixel outside of frame
    img[pos:pos+3] = rgb
    return img

def add_mouse(img, w):
    flags, hcursor, (cx,cy) = win32gui.GetCursorInfo()
    cursor = get_cursor(hcursor)
    cursor_mean = cursor.mean(-1)
    where = np.where(cursor_mean>0)
    for x, y in zip(where[0], where[1]):
        rgb = [x for x in cursor[x,y]]
        img = set_pixel(img, w, x+cy, y+cx, rgb=rgb)
    return img


def get_cursor(hcursor):
    info = win32gui.GetCursorInfo()
    hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
    hbmp = win32ui.CreateBitmap()
    hbmp.CreateCompatibleBitmap(hdc, 36, 36)
    hdc = hdc.CreateCompatibleDC()
    hdc.SelectObject(hbmp)
    hdc.DrawIcon((0,0), hcursor)
    
    bmpinfo = hbmp.GetInfo()
    bmpbytes = hbmp.GetBitmapBits()
    bmpstr = hbmp.GetBitmapBits(True)
    im = np.array(Image.frombuffer(
        'RGB',
         (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
         bmpstr, 'raw', 'BGRX', 0, 1))
    
    win32gui.DestroyIcon(hcursor)    
    win32gui.DeleteObject(hbmp.GetHandle())
    hdc.DeleteDC()
    return im

with mss.mss() as sct:
    screen = sct.monitors[0]
    img = bytearray(sct.grab(screen).rgb)
    img_with_mouse = add_mouse(img, screen['width'])

skjerns avatar Jan 30 '20 21:01 skjerns

Maybe the best reference for linux would be ffmpeg, where mouse capture is implemented as part of screen grabbing (taking a screenshot).

For just getting the mouse coordinates at the time of making a screenshot, using python-xlib:

from Xlib import display
pointer = display.Display().screen().root.query_pointer()
position = (pointer.root_x, pointer.root_y)

But then you have to draw a cursor yourself into the screen grab, and you do not know the actual cursor image used by the display at the particular moment. Cursor rendering is its own little kingdom in X11 ...

matanox avatar May 21 '20 19:05 matanox

Here is how ffmpeg goes about it. It may seem however, that python-xlib does not expose the xfixes API as of now, nor does ffmpeg-python expose that ffmpeg function retrieving the pointer image.

matanox avatar May 21 '20 20:05 matanox

It should be possible to use ctypes to access the XFixesGetCursorImage function from libXfixes.so to get back a XFixesCursorImage C structure like here, but in python.

See also the structure of the image struct here or in the original X source ...

matanox avatar May 21 '20 21:05 matanox

I found a solution but it depends on GTK2 for now and it is written in python2 using x.py and xlib.py from http://code.google.com/p/pyxlib-ctypes/ here is a working exemple https://github.com/zorvios/X11CursorImagePy2

zorvios avatar May 26 '20 09:05 zorvios

I've written a working module for linux, that get the cursor image using Ctypes : https://github.com/zorvios/PyXCursor is there anything we could do to add this feature ?

zorvios avatar Jun 04 '20 17:06 zorvios

Just to add my two cents: When trying to display the mouse via the "grab mouse position on screen and paste mouse icon" method, how to go about scenarios where an application uses custom mouse icons? Would one need to search for each application and see if it uses a custom mouse? Some applications even have more mouse states (or different ones) than OSs does. I know ffmpeg is agnostic to that issue, but don't know how the library does it.

thiagoribeirodamotta avatar Oct 01 '20 14:10 thiagoribeirodamotta

if using PIL for processing a workaround could be the one here: https://github.com/swharden/pyScreenCapture/blob/master/go.py

amadeok avatar Jun 01 '21 08:06 amadeok

v8.0.0 added Linux support. Thanks to @zorvios it introduces all bricks needed for upcoming Mac, and Windows, supports (like MSSBase._merge(screenshot, cursor)).

BoboTiG avatar Apr 05 '23 17:04 BoboTiG

with_cursor=True is only supported in linux. not in windows. Can it be supported in windows? thx

basket-ball avatar Aug 25 '23 13:08 basket-ball

Windows support is ongoing with #272.

BoboTiG avatar Jan 25 '24 20:01 BoboTiG