Python Desktop Capture

Python Desktop Capture

This post uses yet another process similar to an earlier post, https://www.cloudacm.com/?p=5414 where the desktop was piped to a MQTT broker. This post will not be restricted to systems with powershell. Instead it will use python libraries to capture the desktop screen and send that data to a MQTT broker.

The following python script uses the mss library. More information about the library can be found here, https://pypi.org/project/mss/. It’s described as “an ultra-fast cross-platform multiple screenshots module in pure python using ctypes”. The simplicity of the script masks its potential.

from mss import MSS
with mss.MSS() as sct:
sct.shot(output='screenshot.png')

The requirement is that Python is used and the library has been installed.

python -m pip install -U --user mss

The following code expands on this by taking the screen shot data and piping it to a MQTT broker. The example has been customized for use in a Windows environment. The hostname variable is drawn from the system as well as addressing limitations with the capture process for certain screen states.

import sys
import ctypes
import mss
import time
import paho.mqtt.client as mqtt
from PIL import Image
import io
import os

# Hide console window on Windows
if sys.platform == 'win32':
ctypes.windll.kernel32.FreeConsole()

# Get hostname once at startup
hostname = os.environ['COMPUTERNAME']

# MQTT setup
client = mqtt.Client()
client.connect('<broker_ip>', <broker_port>, 60)
client.loop_start()

with mss.mss() as sct:
while True:
try:
monitor = sct.monitors[1]
screenshot = sct.grab(monitor)

img = Image.frombytes('RGB', screenshot.size, screenshot.rgb)
img_resized = img.resize((320, 180), Image.Resampling.LANCZOS)

# Save to bytes buffer instead of disk
img_bytes = io.BytesIO()
img_resized.save(img_bytes, format='PNG')
img_bytes.seek(0)

# Publish image bytes directly to MQTT
# Publish using dynamic hostname
client.publish(f'{hostname}/Desktop', img_bytes.getvalue())

except mss.exception.ScreenShotError:
# BitBlt failed - UAC dialog, lock screen, or secure desktop active
# Skip this capture and try again next cycle
pass

time.sleep(2)

client.loop_stop()

Installing Python and the required libraries may not be practical for target systems. Python features also support compilation into an executable, below is an example command.

python -m PyInstaller --onefile --windowed --noconsole CaptureToMQTT.py

The binary can then be distributed and launched as needed.

 

Comments are closed.