python 实现截图

雨啸青锋 发布于 2020-04-12 711 次阅读


python 截图可以用PIL 的 ImageGrab

PIL下载

pip install pillow

导入

>>> from PIL import ImageGrab 导入图形库
>>> i = ImageGrab.grab()#全屏截图
>>> i.show()#显示图片
自定义位置
x1,y1左上角 x2,y2右下角
i = ImageGrab.grab(bbox = (x1,y1,x2,y2))
i = ImageGrab.grab(bbox = (0,50,1000,1000))

 

tkinter + pillow 自定义截图,没办法tkinter 只支持主窗口透明,所以用的时候点击移动鼠标就行,看不见边框

from tkinter import *
from PIL import ImageGrab


win = Tk()
win.attributes("-fullscreen",True)
win.attributes("-alpha", 0.01)
win.config(bg = "#080808")
ca = Canvas(win)
ca.pack(expand = YES,fill = BOTH)

x = IntVar()
y = IntVar()

def go(event):
    x.set(event.x)
    y.set(event.y)

def move(event):
    x2 = event.x
    y2 = event.y
    x1 = x.get()
    y1 = y.get()
    ca.delete("sq")
    ca.create_rectangle(x1, y1,x2, y2,tags="sq",fill = "#FFFFFF")

def rel(event):
    ca.delete("sq")
    x1 = x.get()
    y1 = y.get()
    x2 = event.x
    y2 = event.y
    i =  ImageGrab.grab(bbox = (x1,y1,x2,y2))
    i.show()
    win.quit()


ca.bind("<Button-1>",go)#点击
ca.bind("<B1 - Motion>",move)#按住移动
ca.bind("<ButtonRelease>",rel)#fk

mainloop()

 

嘿,大家好,小名尧尧,网名雨啸青锋,00后的小站主,某校高中生
最后更新于 2020-04-12