Advertisement
here2share

# Tk_sizable_screengrab.py

Jun 13th, 2015
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. # Tk_sizable_screengrab.py
  2.  
  3. import os, datetime
  4.  
  5. def take_screenshot(x=0, y=0, width=None, height=None):
  6.     try:
  7.         import wx
  8.     except ImportError as e:
  9.         return 'Screenshot could not be taken - wx could not be imported: %s' %(e)
  10.     folder_name = datetime.date.today().strftime('%Y-%m-%d')
  11.     file_name = datetime.datetime.now().strftime('%H-%M-%S') + '.jpg'
  12.     directory = 'C:\\Users\\test\\' + folder_name
  13.     try:
  14.         os.mkdir(directory)
  15.     except: pass
  16.     filename = os.path.join(directory, file_name)
  17.     global filename
  18.  
  19.     app = wx.App()
  20.     screen = wx.ScreenDC()
  21.  
  22.     size = screen.GetSize()
  23.     if width == None:
  24.         width = size[0]
  25.     if height == None:
  26.         height = size[1]
  27.  
  28.     bmp = wx.EmptyBitmap(width, height)
  29.     mem = wx.MemoryDC(bmp)
  30.     mem.Blit(0, 0, width, height, screen, x, y)
  31.  
  32.     del mem
  33.     bmp.SaveFile(filename, wx.BITMAP_TYPE_PNG)
  34.    
  35. take_screenshot(0,0,800,600)
  36. print 'Screenshot saved to file: %s' %(filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement