python_屏幕截图_区域截图_快捷键

import time,io
from PIL import ImageGrab, Image
import win32clipboard,win32con
from pynput import keyboard, mouse

folderpath="D:/小姐姐截图/"

def on_release(key):
  if key == keyboard.Key.print_screen:
    screenRegion()


# 区域截图left, upper, right, lower
def screenRegion():
  try:
    left, top = 34, 54
    width, height = 375, 667  # iphone6
    bbox = (left, top, left + width, top + height)
    img = ImageGrab.grab(bbox)
    newfilename = "{}{}.jpg".format(folderpath,int(time.time() * 1000))
    img.save(newfilename)
    
    # 将图片转换为字节流
    output = io.BytesIO()
    img.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    setClipboard(data)
    print("screen saved!")
  except Exception as e:
    print("error:",e)


# 往剪贴板中放入图片
def setClipboard(data):
  win32clipboard.OpenClipboard()  # 打开剪贴板
  win32clipboard.EmptyClipboard()  # 先清空剪贴板
  win32clipboard.SetClipboardData(win32con.CF_DIB, data)  # 将图片放入剪贴板
  win32clipboard.CloseClipboard()

if __name__ == '__main__':
  listener = keyboard.Listener(on_release=on_release)
  listener.start()
  listener.join()

 

posted @ 2020-02-26 00:48  enumx  阅读(1810)  评论(0编辑  收藏  举报