{"id":20060,"date":"2021-07-20T15:03:12","date_gmt":"2021-07-20T15:03:12","guid":{"rendered":"https:\/\/www.askpython.com\/?p=20060"},"modified":"2023-06-16T11:10:50","modified_gmt":"2023-06-16T11:10:50","slug":"capture-screenshots","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/capture-screenshots","title":{"rendered":"2 Ways to Capture Screenshots Using Python"},"content":{"rendered":"\n<p>In this tutorial, we will learn how can one take screenshots using the Python programming language. There are multiple ways to achieve the same, we will be discussing some of them in the coming sections.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to capture screenshots using Python<\/h2>\n\n\n\n<p>Python offers various libraries to capture screenshots. We&#8217;ll be exploring a few of these libraries today and understand how you can implement the code in Python to capture your screens.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 1: Using pyautogui module<\/h2>\n\n\n\n<p>The <code>pyautogui<\/code> module makes use of the <code>screenshot<\/code> function which is responsible for taking the screenshot of the whole computer screen. And then the <code>save<\/code> function is used to save the screenshot captured to our device.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport pyautogui\nim = pyautogui.screenshot()\nim.save(&quot;SS1.jpg&quot;)\n<\/pre><\/div>\n\n\n<p>The image saved would look something like this.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1920\" height=\"1080\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/Screenshot_Python_1.jpg\" alt=\"capture screenshots\" class=\"wp-image-20755\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/Screenshot_Python_1.jpg 1920w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/Screenshot_Python_1-300x169.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/Screenshot_Python_1-1024x576.jpg 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/Screenshot_Python_1-768x432.jpg 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/Screenshot_Python_1-1536x864.jpg 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><figcaption class=\"wp-element-caption\">Screenshot Python 1<\/figcaption><\/figure>\n\n\n\n<p>If one wants some delay before taking an automated screenshot, the programmer can make use of the <code>time<\/code> module and making use of the <code>sleep<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 2: Using pillow module<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/www.askpython.com\/python-modules\/pillow-module\" data-type=\"post\" data-id=\"17924\">pillow module<\/a> makes use of  an <code>ImageGrab<\/code> submodule. This method requires a region that needs to be captured which implies setting the diagonal coordinates of the region.<\/p>\n\n\n\n<p>Then we make use of the <code>grab<\/code> function which will take the region parameters to capture the screenshot. And finally, save the captured image using the <code>save<\/code> function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nfrom PIL import ImageGrab\nss_region = (300, 300, 600, 600)\nss_img = ImageGrab.grab(ss_region)\nss_img.save(&quot;SS3.jpg&quot;)\n<\/pre><\/div>\n\n\n<p>The region captured is shown below. We can also make use of the <code>time<\/code> module to delay the capturing of the screenshot.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/Screenshot_Python_2.jpg\" alt=\"capture screenshots\" class=\"wp-image-20758\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/Screenshot_Python_2.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/07\/Screenshot_Python_2-150x150.jpg 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption class=\"wp-element-caption\">Screenshot Python 2<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So now you know two methods to capture screenshots of your computer screen. And yes there are other methods as well as python is a very advanced language. Hope you liked reading it!<\/p>\n\n\n\n<p>Thank you for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn how can one take screenshots using the Python programming language. There are multiple ways to achieve the same, we will be discussing some of them in the coming sections. How to capture screenshots using Python Python offers various libraries to capture screenshots. We&#8217;ll be exploring a few of these [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":20770,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-20060","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/20060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=20060"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/20060\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/20770"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=20060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=20060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=20060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}