BUAA-Soft-2024-Summer / Soft-Summer-2024

北航软件学院 2024 夏《程序设计实践》 小学期仓库
11 stars 1 forks source link

如何用Python实现截图功能?(已解决) #3

Closed moimou93 closed 2 months ago

moimou93 commented 2 months ago

当运行程序时,可以利用鼠标拖拽自行选取要截图的范围(类似于微信/QQ截图的效果),选定好截图范围后 将图片传入到变量中/将其保存到指定位置

Lord-Turmoil commented 2 months ago

对于截图功能,可以使用 Pillow 实现,截取部分屏幕范围的代码如下,不指定范围即可截取整个屏幕。

from PIL import ImageGrab

# Set the region to take a screenshot of
bbox = (10, 10, 510, 510)

# Take a screenshot of the specified region
pic = ImageGrab.grab(bbox)

# Display the screenshot
pic.show()

# Save the screenshot to a file
pic.save("screenshot.png")

但是这种方式无法直接让用户选择截图范围。要实现这一点,可以参考一般截图软件的做法:

  1. 首先截取整个屏幕
  2. 将截图全屏展示,让用户选择
  3. 根据用户选择的区域截取图片
moimou93 commented 2 months ago

感谢@Lord-Turmoil 助教提供的思路 可以干脆参考截图工具的方法,顺藤摸瓜找到https://github.com/fandesfyf/Jamscreenshot 项目 项目提供的方法可以解决本问题