{"id":25886,"date":"2021-12-16T09:20:59","date_gmt":"2021-12-16T03:50:59","guid":{"rendered":"https:\/\/python-programs.com\/?p=25886"},"modified":"2021-12-16T09:20:59","modified_gmt":"2021-12-16T03:50:59","slug":"python-program-to-capture-screenshots","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-capture-screenshots\/","title":{"rendered":"Python Program to Capture Screenshots"},"content":{"rendered":"
There are numerous approaches to capture the screenshots in Python.<\/p>\n
Python includes a number of libraries for capturing screenshots. In this article, let us look at a handful of these libraries and their implementation using Python code.<\/p>\n
The pyautogui module employs the screenshot function, which is in charge of capturing a snapshot of the entire computer screen. The save function is then utilized to save the screenshot to our device.<\/p>\n
An ImageGrab submodule is used by the pillow module. This method necessitates the capture of a region, which necessitates the setting of the region’s diagonal coordinates.<\/p>\n
Then we use the grab function, which takes the region parameters and uses them to capture the screenshot. Finally, use the save function to save the taken image.<\/p>\n
Before going to the coding part, install the pyautogui module in your system as shown below:<\/p>\n
pip\u00a0install\u00a0pyautogui<\/pre>\n<\/div>\nOutput:<\/strong><\/p>\n
Collecting pyautogui Downloading PyAutoGUI-0.9.53.tar.gz (59 kB) \r\n|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 59 kB 3.7 MB\/s Collecting pymsgbox \r\nDownloading PyMsgBox-1.0.9.tar.gz (18 kB) Installing build dependencies ... \r\ndone Getting requirements to build wheel ... done Preparing wheel metadata ... \r\ndone Collecting PyTweening>=1.0.1 Downloading pytweening-1.0.4.tar.gz (14 kB) \r\nCollecting pyscreeze>=0.1.21 Downloading PyScreeze-0.1.28.tar.gz (25 kB) \r\nInstalling build dependencies ... done Getting requirements to build wheel ... \r\ndone Preparing wheel metadata ... done Collecting pygetwindow>=0.0.5 Downloading\r\n PyGetWindow-0.0.9.tar.gz (9.7 kB) Collecting mouseinfo Downloading MouseInfo-0.1\r\n.3.tar.gz (10 kB) Collecting python3-Xlib Downloading python3-xlib-0.15.tar.gz \r\n(132 kB) |\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 132 kB 18.8 MB\/s Collecting pyrect \r\nDownloading PyRect-0.1.4.tar.gz (15 kB) Requirement already satisfied: Pillow>=5\r\n.2.0 in \/usr\/local\/lib\/python3.7\/dist-packages (from pyscreeze>=0.1.21->\r\npyautogui) (7.1.2) Collecting pyperclipBuilding wheel for pyrect (setup.py) ...\r\ndone Created wheel for pyrect: filename=PyRect-0.1.4-py2.py3-none-any.whl size=95\r\n47 sha256=ed4a10cef4885f276a3067e64ab033e9507adea168952f5ce4636cb3a6f6d12e \r\nStored in directory: \/root\/.cache\/pip\/wheels\/97\/5f\/8e\/6f26a5b00d46679ee2391a3542\r\n334274ce8bdaf7c6b0f3504c Building wheel for python3-Xlib (setup.py) ... done\r\nCreated wheel for python3-Xlib: filename=python3_xlib-0.15-py3-none-any.whl \r\nsize=109517 sha256=9844de06645f2fc25ed8ae389ad41da84bbe8ed102c6013ce3b88c10c\r\n5e216df Stored in directory: \/root\/.cache\/pip\/wheels\/67\/6f\/f2\/18f51230840318\r\ne784c45e1392a0e174777e499251e42ddf86 Successfully built pyautogui pygetwindow\r\npyscreeze PyTweening mouseinfo pymsgbox pyperclip pyrect python3-Xlib \r\nInstalling collected packages: python3-Xlib, pyrect, pyperclip, PyTweening, \r\npyscreeze, pymsgbox, pygetwindow, mouseinfo, pyautogui Successfully installed \r\nPyTweening-1.0.4 mouseinfo-0.1.3 pyautogui-0.9.53 pygetwindow-0.0.9 pymsgbox-1\r\n.0.9 pyperclip-1.8.2 pyrect-0.1.4 pyscreeze-0.1.28 python3-Xlib-0.15<\/pre>\nMethod #1: Using Built-in Functions (Static Input)<\/h3>\n
1)By Using pyautogui module<\/strong><\/p>\n
Approach:<\/strong><\/p>\n
\n
- Import pyautogui module using the import keyword.<\/li>\n
- Take the screenshot using the pyautogui.screenshot() function and\u00a0store it in a variable<\/li>\n
- Save the above screenshot using the save() function<\/li>\n
- The Exit of the Program.<\/li>\n<\/ul>\n
Below is the implementation:<\/strong><\/p>\n
# Import pyautogui module using the import keyword.\r\nimport pyautogui\r\n# Take the screenshot using the pyautogui.screenshot() function and \r\n# store it in a variable\r\nscrnsht = pyautogui.screenshot()\r\n# Save the above screenshot using the save() function\r\nscrnsht.save(\"SS1.jpg\")<\/pre>\n2) By using pillow Module<\/strong><\/p>\n
Approach:<\/strong><\/p>\n
\n
- Import ImageGrab function from PIL module using the import keyword.<\/li>\n
- Give the diagonal coordinates as static input and store it in a variable.<\/li>\n
- Pass the above given diagonal coordinates to the ImageGrab.grab() function to take a screenshot and store it in another variable.<\/li>\n
- Save the above screenshot using the save() function.<\/li>\n
- The Exit of the Program.<\/li>\n<\/ul>\n
Below is the implementation:<\/strong><\/p>\n
# Import ImageGrab function from PIL module using the import keyword.\r\nfrom PIL import ImageGrab\r\n# Give the diagonal coordinates as static input and store it in a variable\r\ndiagnl_cordnates = (200, 200, 400, 400)\r\n# Pass the above given diagonal coordinates to the ImageGrab.grab() function\r\n# to take a screenshot and store it in another variable\r\nscrnsht = ImageGrab.grab(diagnl_cordnates)\r\n# Save the above screenshot using the save() function.\r\nscrnsht.save(\"myscreenshot.jpg\")\r\n<\/pre>\n<\/p>\n
<\/p>\n","protected":false},"excerpt":{"rendered":"
There are numerous approaches to capture the screenshots in Python. Python includes a number of libraries for capturing screenshots. In this article, let us look at a handful of these libraries and their implementation using Python code. 1)By Using pyautogui module The pyautogui module employs the screenshot function, which is in charge of capturing a …<\/p>\n