Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from PIL import Image, ImageGrab
- from tkinter import filedialog
- import datetime
- from io import BytesIO
- import win32clipboard
- def show(mode):
- root = Tk()
- root['bg'] = 'black'
- root.attributes('-alpha',0.6)
- root.attributes('-fullscreen',1)
- root.attributes('-transparentcolor','green')
- root.attributes('-topmost',1)
- root.bind('<Escape>',lambda e: root.destroy())
- root['cursor'] = 'crosshair'
- output = BytesIO()
- def start_rect(event):
- global rectx0,recty0
- rectx0 = canvas.canvasx(event.x)
- recty0 = canvas.canvasx(event.y)
- canvas.create_rectangle(rectx0, recty0, rectx0, recty0, fill="green",tag='rect')
- def draw_rect(event):
- rectx1 = canvas.canvasx(event.x)
- recty1 = canvas.canvasx(event.y)
- canvas.coords('rect',rectx0,recty0,rectx1,recty1)
- def stop_rect(event):
- rectx1 = canvas.canvasx(event.x)
- recty1 = canvas.canvasx(event.y)
- canvas.coords('rect',rectx0,recty0,rectx1,recty1)
- capture('rect')
- def capture(obj):
- # print('The points are: ',canvas.coords(obj))
- x0,y0,x1,y1 = canvas.bbox(obj)
- canvas.delete(obj)
- img = ImageGrab.grab(bbox=(x0,y0,x1,y1))
- save(img,mode)
- def save(img,mode):
- if mode == 's':
- close()
- today = f"Screenshot {datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d %H%M%S')}"
- dir = filedialog.asksaveasfilename(defaultextension='.png',filetypes=[('PNG file','*.png')],initialfile=today)
- if dir:
- img.save(dir)
- root.destroy()
- else:
- open()
- elif mode == 'c':
- img.convert('RGB').save(output,"BMP")
- data = output.getvalue()[14:]
- output.close()
- win32clipboard.OpenClipboard()
- win32clipboard.EmptyClipboard()
- win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
- win32clipboard.CloseClipboard()
- root.destroy()
- def close():
- root.withdraw()
- def open():
- root.deiconify()
- canvas = Canvas(root,bg='black')
- canvas.pack(expand=1,fill='both')
- root.bind('<Button-1>',start_rect)
- root.bind('<ButtonRelease-1>',stop_rect)
- root.bind('<B1-Motion>',draw_rect)
- root.mainloop()
- import keyboard
- # press ctrl+shift+z to print "Hotkey Detected"
- keyboard.add_hotkey('ctrl + shift + z', show, args=('s'))
- keyboard.add_hotkey('ctrl + shift + x', show, args=('c'))
- keyboard.wait('esc')
Advertisement
Add Comment
Please, Sign In to add comment