Capture cursor on Windows
This commit is contained in:
@@ -14,7 +14,7 @@ import lxml.etree
|
|||||||
import pyautogui
|
import pyautogui
|
||||||
import requests
|
import requests
|
||||||
import re
|
import re
|
||||||
from PIL import Image
|
from PIL import Image, ImageGrab
|
||||||
from Xlib import display, X
|
from Xlib import display, X
|
||||||
from flask import Flask, request, jsonify, send_file, abort # , send_from_directory
|
from flask import Flask, request, jsonify, send_file, abort # , send_from_directory
|
||||||
from lxml.etree import _Element
|
from lxml.etree import _Element
|
||||||
@@ -35,6 +35,7 @@ elif platform_name == "Windows":
|
|||||||
from pywinauto import Desktop
|
from pywinauto import Desktop
|
||||||
from pywinauto.base_wrapper import BaseWrapper
|
from pywinauto.base_wrapper import BaseWrapper
|
||||||
import pywinauto.application
|
import pywinauto.application
|
||||||
|
import win32ui, win32gui
|
||||||
|
|
||||||
Accessible = Any
|
Accessible = Any
|
||||||
|
|
||||||
@@ -88,7 +89,7 @@ def execute_command():
|
|||||||
# Execute the command without any safety checks.
|
# Execute the command without any safety checks.
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell, text=True,
|
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell, text=True,
|
||||||
timeout=120)
|
timeout=120, creationflags=subprocess.CREATE_NO_WINDOW)
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'status': 'success',
|
'status': 'success',
|
||||||
'output': result.stdout,
|
'output': result.stdout,
|
||||||
@@ -150,22 +151,47 @@ def capture_screen_with_cursor():
|
|||||||
|
|
||||||
# fixme: This is a temporary fix for the cursor not being captured on Windows and Linux
|
# fixme: This is a temporary fix for the cursor not being captured on Windows and Linux
|
||||||
if user_platform == "Windows":
|
if user_platform == "Windows":
|
||||||
def _download_image(url, path):
|
def get_cursor():
|
||||||
response = requests.get(url)
|
hcursor = win32gui.GetCursorInfo()[1]
|
||||||
with open(path, 'wb') as file:
|
hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
|
||||||
file.write(response.content)
|
hbmp = win32ui.CreateBitmap()
|
||||||
|
hbmp.CreateCompatibleBitmap(hdc, 36, 36)
|
||||||
cursor_path = os.path.join("screenshots", "cursor.png")
|
hdc = hdc.CreateCompatibleDC()
|
||||||
if not os.path.exists(cursor_path):
|
hdc.SelectObject(hbmp)
|
||||||
cursor_url = "https://vip.helloimg.com/images/2023/12/02/oQPzmt.png"
|
hdc.DrawIcon((0,0), hcursor)
|
||||||
_download_image(cursor_url, cursor_path)
|
|
||||||
screenshot = pyautogui.screenshot()
|
bmpinfo = hbmp.GetInfo()
|
||||||
cursor_x, cursor_y = pyautogui.position()
|
bmpstr = hbmp.GetBitmapBits(True)
|
||||||
cursor = Image.open(cursor_path)
|
cursor = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1).convert("RGBA")
|
||||||
# make the cursor smaller
|
|
||||||
cursor = cursor.resize((int(cursor.width / 1.5), int(cursor.height / 1.5)))
|
win32gui.DestroyIcon(hcursor)
|
||||||
screenshot.paste(cursor, (cursor_x, cursor_y), cursor)
|
win32gui.DeleteObject(hbmp.GetHandle())
|
||||||
screenshot.save(file_path)
|
hdc.DeleteDC()
|
||||||
|
|
||||||
|
pixdata = cursor.load()
|
||||||
|
|
||||||
|
width, height = cursor.size
|
||||||
|
for y in range(height):
|
||||||
|
for x in range(width):
|
||||||
|
if pixdata[x, y] == (0, 0, 0, 255):
|
||||||
|
pixdata[x, y] = (0, 0, 0, 0)
|
||||||
|
|
||||||
|
hotspot = win32gui.GetIconInfo(hcursor)[1:3]
|
||||||
|
|
||||||
|
return (cursor, hotspot)
|
||||||
|
|
||||||
|
cursor, (hotspotx, hotspoty) = get_cursor()
|
||||||
|
|
||||||
|
ratio = ctypes.windll.shcore.GetScaleFactorForDevice(0) / 100
|
||||||
|
|
||||||
|
img = ImageGrab.grab(bbox=None, include_layered_windows=True)
|
||||||
|
|
||||||
|
pos_win = win32gui.GetCursorPos()
|
||||||
|
pos = (round(pos_win[0]*ratio - hotspotx), round(pos_win[1]*ratio - hotspoty))
|
||||||
|
|
||||||
|
img.paste(cursor, pos, cursor)
|
||||||
|
|
||||||
|
img.save(file_path)
|
||||||
elif user_platform == "Linux":
|
elif user_platform == "Linux":
|
||||||
cursor_obj = Xcursor()
|
cursor_obj = Xcursor()
|
||||||
imgarray = cursor_obj.getCursorImageArrayFast()
|
imgarray = cursor_obj.getCursorImageArrayFast()
|
||||||
|
|||||||
Reference in New Issue
Block a user