{"id":5456,"date":"2026-10-28T08:00:41","date_gmt":"2026-10-28T15:00:41","guid":{"rendered":"https:\/\/www.cloudacm.com\/?p=5456"},"modified":"2026-10-23T05:57:05","modified_gmt":"2026-10-23T12:57:05","slug":"python-desktop-capture","status":"publish","type":"post","link":"https:\/\/www.cloudacm.com\/?p=5456","title":{"rendered":"Python Desktop Capture"},"content":{"rendered":"<p>This post uses yet another process similar to an earlier post, <a href=\"https:\/\/www.cloudacm.com\/?p=5414\">https:\/\/www.cloudacm.com\/?p=5414<\/a> 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.<\/p>\n<p>The following python script uses the mss library. More information about the library can be found here, <a href=\"https:\/\/pypi.org\/project\/mss\/\">https:\/\/pypi.org\/project\/mss\/<\/a>. It&#8217;s described as &#8220;an ultra-fast cross-platform multiple screenshots module in pure python using ctypes&#8221;. The simplicity of the script masks its potential.<\/p>\n<pre>from mss import MSS\r\nwith mss.MSS() as sct:\r\nsct.shot(output='screenshot.png')\r\n<\/pre>\n<p>The requirement is that Python is used and the library has been installed.<\/p>\n<pre>python -m pip install -U --user mss\r\n<\/pre>\n<p>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.<\/p>\n<pre>import sys\r\nimport ctypes\r\nimport mss\r\nimport time\r\nimport paho.mqtt.client as mqtt\r\nfrom PIL import Image\r\nimport io\r\nimport os\r\n\r\n# Hide console window on Windows\r\nif sys.platform == 'win32':\r\nctypes.windll.kernel32.FreeConsole()\r\n\r\n# Get hostname once at startup\r\nhostname = os.environ['COMPUTERNAME']\r\n\r\n# MQTT setup\r\nclient = mqtt.Client()\r\nclient.connect('&lt;broker_ip&gt;', &lt;broker_port&gt;, 60)\r\nclient.loop_start()\r\n\r\nwith mss.mss() as sct:\r\nwhile True:\r\ntry:\r\nmonitor = sct.monitors[1]\r\nscreenshot = sct.grab(monitor)\r\n\r\nimg = Image.frombytes('RGB', screenshot.size, screenshot.rgb)\r\nimg_resized = img.resize((320, 180), Image.Resampling.LANCZOS)\r\n\r\n# Save to bytes buffer instead of disk\r\nimg_bytes = io.BytesIO()\r\nimg_resized.save(img_bytes, format='PNG')\r\nimg_bytes.seek(0)\r\n\r\n# Publish image bytes directly to MQTT\r\n# Publish using dynamic hostname\r\nclient.publish(f'{hostname}\/Desktop', img_bytes.getvalue())\r\n\r\nexcept mss.exception.ScreenShotError:\r\n# BitBlt failed - UAC dialog, lock screen, or secure desktop active\r\n# Skip this capture and try again next cycle\r\npass\r\n\r\ntime.sleep(2)\r\n\r\nclient.loop_stop()\r\n<\/pre>\n<p>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.<\/p>\n<pre>python -m PyInstaller --onefile --windowed --noconsole CaptureToMQTT.py\r\n<\/pre>\n<p>The binary can then be distributed and launched as needed.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s described as &#8220;an ultra-fast cross-platform multiple screenshots module in pure python&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.cloudacm.com\/?p=5456\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5456","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/5456","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5456"}],"version-history":[{"count":5,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/5456\/revisions"}],"predecessor-version":[{"id":5458,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/5456\/revisions\/5458"}],"wp:attachment":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}