{"id":2424,"date":"2015-09-30T13:25:36","date_gmt":"2015-09-30T13:25:36","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2424"},"modified":"2015-09-30T13:25:36","modified_gmt":"2015-09-30T13:25:36","slug":"how-to-take-a-screenshot-with-python","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/30\/how-to-take-a-screenshot-with-python\/","title":{"rendered":"How to take a screenshot with Python"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushPython.js\"><\/script>\n<p>This is a small platform-independent python script to take a screenshot of the entire screen. The code is in Python 2.7 and requires the wxPython library which is available in all platforms. By default, it takes an instant screenshot of the screen and saves it as screenshot.png in the same folder as the script. Two command line arguments can be passed as:<\/p>\n<p><em>\u00a0 python sshot.py &lt;delay in seconds&gt; &lt;filename to save in&gt;<\/em><\/p>\n<p>Delay delays the screenshot by the designated seconds so that you can arrange the screen or windows as required.<\/p>\n<p>The filename argument will save the screenshot in the file &lt;filename&gt;.png . Note that the png suffix is assigned automatically.<\/p>\n<p>&nbsp;<\/p>\n<p><pre class=\"brush: python\">#\r\n# Take screenshot of screen\r\n# Usage &lt;delay in seconds&gt; &lt;filename&gt;\r\n#\r\n# delay should be a valid number\r\n# filename should not have an extension as it will be saved as a png\r\n\r\n# Python 2.7\r\n#\r\n\r\nimport wx\r\nimport time\r\nimport sys\r\n\r\ntime_delay = 0\r\nfile_name = &quot;screenshot&quot;\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n\tapp = wx.App()\r\n\t\r\n\t# check for args being passed\r\n\tif len(sys.argv) &gt; 1:\r\n\t\t# delay in seconds\r\n\t\tif len(sys.argv) &gt;= 2:\r\n\t\t\ttime_delay = sys.argv[1]\r\n\t\t\t\r\n\t\t\t#filename \r\n\t\t\tif len(sys.argv) &gt;= 3:\r\n\t\t\t\tfile_name = sys.argv[2]\r\n\r\n\tif time_delay &gt; 0:\r\n\t\ttime.sleep(float(time_delay))\r\n\r\n\t# capture screen and save it to file\r\n\tscreen = wx.ScreenDC()\r\n\tsize = screen.GetSize()\r\n\tbmp = wx.EmptyBitmap(size[0], size[1])\r\n\tmem = wx.MemoryDC(bmp)\r\n\tmem.Blit(0,0,size[0], size[1], screen, 0, 0)\r\n\tdel mem\r\n\tbmp.SaveFile(file_name + &quot;.png&quot;, wx.BITMAP_TYPE_PNG)\r\n\r\n\t#show alert message\r\n\tdlg = wx.MessageDialog(None, &quot;Screenshot was saved as &quot; + file_name + &quot;.png&quot;, &quot;Alert&quot;, wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP)\r\n\tdlg.ShowModal()\r\n\tdlg.Destroy()\r\n\r\n\r\n<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>This is a small platform-independent python script to take a screenshot of the entire screen. The code is in Python 2.7 and requires the wxPython <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/30\/how-to-take-a-screenshot-with-python\/\" title=\"How to take a screenshot with Python\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-2424","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2424"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2424\/revisions"}],"predecessor-version":[{"id":2427,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2424\/revisions\/2427"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2107"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}