社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

【kimol君的无聊小发明】—用python写截屏小工具

不正经的kimol君 • 4 年前 • 330 次点击  

前言

今天我看了一下自己的文件夹,发现了自己写了许多似乎很无聊的代码。于是乎,一个想法油然而生:“生活已经很无聊了,不如再无聊一点叭”。
说干就干,那就开一个专题,我们称之为 kimol君的无聊小发明
妙…啊~~~

今天要做的东西,就是用python写一个截屏工具。想必大家平时截屏的时候很多都是用的微信的ALT+A或者是WINDOWS自带的截屏软件。作为技术宅的我,怎么总是用别人的东西呢?
看我的,上图:
在这里插入图片描述
效果还阔以,好歹这个小猫咪截出来也是有模有样的~

一、技术实现

1. 相关库

在python中可以实现截图的库有很多,这里我用的是pyautogui,它可以对指定区域进行截屏:

import pyautogui
  • 1
  • 1

而界面部分(截屏的框选等)采用了tkinter库:

import tkinter as tk
  • 1
  • 1

2. 定义窗口

定义一个透明的窗口,让它的大小与屏幕一样:

root = tk.Tk()
root.overrideredirect(True)         # 隐藏窗口的标题栏
root.attributes("-alpha", 0.1)      # 窗口透明度10%
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
root.configure(bg="black")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

注: alhpa参数用于设定透明度;geometry函数用于设定窗口大小;
创建一个子窗口,用于显示框选区域:

# 再创建1个Canvas用于圈选
cv = tk.Canvas(root)
x, y = 0, 0
xstart,ystart = 0 ,0
xend,yend = 0, 0
rec = ''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. 绘制工具条

绘制一个工具条,方便后续增加功能。讲道理目前来说这个就是个鸡肋(可以省去),但是如果后续还想晚上功能的话,会很有帮助。

canvas = tk.Canvas(root)
canvas.configure(width=300)
canvas.configure(height=100)
canvas.configure(bg="yellow")
canvas.configure(highlightthickness=0)  # 高亮厚度
canvas.place(x=(root.winfo_screenwidth()-500),y=(root.


    
winfo_screenheight()-300))
canvas.create_text(150, 50,font='Arial -20 bold',text='ESC退出,假装工具条')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4. 定义事件函数

监听鼠标和键盘事件,实时监控框选区域和截屏,具体的事件有:

# 绑定事件
canvas.bind("<B1-Motion>", move)   # 鼠标左键移动->显示当前光标位置
root.bind('<Escape>',sys_out)      # 键盘Esc键->退出
root.bind("<Button-1>", button_1)  # 鼠标左键点击->显示子窗口 
root.bind("<B1-Motion>", b1_Motion)# 鼠标左键移动->改变子窗口大小
root.bind("<ButtonRelease-1>", buttonRelease_1) # 鼠标左键释放->记录最后光标的位置
root.bind("<Button-3>",button_3)   #鼠标右键点击->截屏并保存图片
root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

监听鼠标光标位置:

def move(event):
    global x, y ,xstart,ystart
    new_x = (event.x-x)+canvas.winfo_x()
    new_y = (event.y-y)+canvas.winfo_y()
    s = "300x200+" + str(new_x)+"+" + str(new_y)    
    canvas.place(x = new_x - xstart,y = new_y -ystart)   
    print("s = ", s)
    print(root.winfo_x(), root.winfo_y())
    print(event.x, event.y)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

创建子窗口:

def button_1(event):
    global x, y ,xstart,ystart
    global rec
    x, y = event.x, event.y
    xstart,ystart = event.x, event.y
    print


    
("event.x, event.y = ", event.x, event.y)
    xstart,ystart = event.x, event.y  
    cv.configure(height=1)
    cv.configure(width=1)
    cv.config(highlightthickness=0) # 无边框
    cv.place(x=event.x, y=event.y)
    rec = cv.create_rectangle(0,0,0,0,outline='red',width=8,dash=(4, 4))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

改变子窗口大小:

def b1_Motion(event):
    global x, y,xstart,ystart
    x, y = event.x, event.y
    print("event.x, event.y = ", event.x, event.y)
    cv.configure(height = event.y - ystart)
    cv.configure(width = event.x - xstart)
    cv.coords(rec,0,0,event.x-xstart,event.y-ystart)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

松开鼠标,记录最后的光标位置:

def buttonRelease_1(event):
    global xend,yend
    xend, yend = event.x, event.y
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

截屏并保存图片:

def button_3(event):
    global xstart,ystart,xend,yend
    cv.delete(rec)
    cv.place_forget()
    img = pyautogui.screenshot(region=[xstart,ystart,xend-xstart,yend-ystart]) # x,y,w,h
    img.save('screenshot.png')
    sys_out(None)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

退出程序:

def sys_out(even):
    root.destroy()
  • 1
  • 2
  • 1
  • 2

二、后续改进

这个还有很多可以改进的地方。

  1. 截图的时候会有dos窗口遮挡 :这个解决办法由很多,类似后台启动,或者将.py编译为可执行文件等等;
  2. 使用不方便,为了截图还的运行代码 :废话!!!当然很不方便呀,不然怎么说是个无聊的小发明呢。但是!!!它也不是一无事处,可以考虑把它放到后台运行。同样可以利用快捷键来唤醒它并完成截图。
  3. 功能单一 :这只是一个很简单的demo,既然可以截屏了,那么我们离录屏还远吗?(正如本文开头的动图,右上角晃晃几个大字“ApowerREC”,不给钱休想去掉,哼~)那么,我们为何不自己写一个呢?

写在最后

最后,感谢各位大大的耐心阅读~
慢着,大侠请留步… 动起可爱的双手,来个赞再走呗 (๑◕ܫ←๑)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/73832
 
330 次点击