Guest User

Screen Snip

a guest
Apr 30th, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. from tkinter import *
  2. from PIL import Image, ImageGrab
  3. from tkinter import filedialog
  4. import datetime
  5. from io import BytesIO
  6. import win32clipboard
  7.  
  8. def show(mode):
  9.     root = Tk()
  10.     root['bg'] = 'black'
  11.     root.attributes('-alpha',0.6)
  12.     root.attributes('-fullscreen',1)
  13.     root.attributes('-transparentcolor','green')
  14.     root.attributes('-topmost',1)
  15.     root.bind('<Escape>',lambda e: root.destroy())
  16.     root['cursor'] = 'crosshair'
  17.     output = BytesIO()
  18.  
  19.     def start_rect(event):
  20.         global rectx0,recty0
  21.  
  22.         rectx0 = canvas.canvasx(event.x)
  23.         recty0 = canvas.canvasx(event.y)
  24.         canvas.create_rectangle(rectx0, recty0, rectx0, recty0, fill="green",tag='rect')
  25.  
  26.     def draw_rect(event):
  27.         rectx1 = canvas.canvasx(event.x)
  28.         recty1 = canvas.canvasx(event.y)
  29.         canvas.coords('rect',rectx0,recty0,rectx1,recty1)
  30.  
  31.     def stop_rect(event):
  32.         rectx1 = canvas.canvasx(event.x)
  33.         recty1 = canvas.canvasx(event.y)
  34.         canvas.coords('rect',rectx0,recty0,rectx1,recty1)
  35.         capture('rect')
  36.  
  37.     def capture(obj):
  38.         # print('The points are: ',canvas.coords(obj))
  39.         x0,y0,x1,y1 = canvas.bbox(obj)
  40.         canvas.delete(obj)
  41.         img = ImageGrab.grab(bbox=(x0,y0,x1,y1))
  42.         save(img,mode)
  43.  
  44.     def save(img,mode):
  45.         if mode == 's':
  46.             close()
  47.            
  48.             today = f"Screenshot {datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d %H%M%S')}"
  49.             dir = filedialog.asksaveasfilename(defaultextension='.png',filetypes=[('PNG file','*.png')],initialfile=today)
  50.             if dir:
  51.                 img.save(dir)
  52.                 root.destroy()
  53.             else:
  54.                 open()
  55.         elif mode == 'c':
  56.             img.convert('RGB').save(output,"BMP")
  57.             data = output.getvalue()[14:]
  58.             output.close()
  59.  
  60.             win32clipboard.OpenClipboard()
  61.             win32clipboard.EmptyClipboard()
  62.             win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
  63.             win32clipboard.CloseClipboard()
  64.             root.destroy()
  65.  
  66.     def close():
  67.         root.withdraw()
  68.  
  69.     def open():
  70.         root.deiconify()
  71.  
  72.     canvas = Canvas(root,bg='black')
  73.     canvas.pack(expand=1,fill='both')
  74.  
  75.     root.bind('<Button-1>',start_rect)
  76.     root.bind('<ButtonRelease-1>',stop_rect)
  77.     root.bind('<B1-Motion>',draw_rect)
  78.  
  79.     root.mainloop()
  80.  
  81. import keyboard
  82.  
  83. # press ctrl+shift+z to print "Hotkey Detected"
  84. keyboard.add_hotkey('ctrl + shift + z', show, args=('s'))
  85. keyboard.add_hotkey('ctrl + shift + x', show, args=('c'))
  86.  
  87. keyboard.wait('esc')
Advertisement
Add Comment
Please, Sign In to add comment