🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
  最近几年,各种论坛回帖中经常会见到这样的回复:无图无真相。还有经常写工作周报或者实验结果时,均要贴几张图上去。所以,抓图的需求在工作和生活中应用算是比较多的了,如果在家,我个人一直使用PrtSc键或QQ的抓图工具,用PrtScr键的一个问题是,我必须打开画图板(mspaint.exe),把剪贴板中的图片黏贴到画布上,再另存为,还是比较麻烦的。如果在公司,因为用不了QQ,此时,要抓个一块区域上的图,还要通过画图板来编辑一下,也是比较浪费时间的。  为解决一上问题,今天使用python写了一个抓图的工具,支持使用快捷键抓取屏幕、当前窗口,所选区域三种需求,抓到之后并弹出保存对话框,再也不用使用画图板中转一下了,进入正题: ### 一、代码: ~~~ #!/usr/bin/env python #coding=gb2312 #此模块主要提供抓图功能,支持以下三种抓图方式: #1、抓取全屏,快捷键CTRL+F1 #2、抓取当前窗口,快捷键CTRL+F2 #3、抓取所选区域,快捷键CTRL+F3 #抓到之后,会自动弹出保存对话框,选择路径保存即可 #*********************** #更新记录 #0.1 2012-03-10 create by dyx1024 #********************** import pyhk import wx import os import sys from PIL import ImageGrab import ctypes import win32gui import ctypes.wintypes def capture_fullscreen(): ''' Function:全屏抓图 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-03-10 ''' #抓图 pic = ImageGrab.grab() #保存图片 save_pic(pic) def capture_current_windows(): ''' Function:抓取当前窗口 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-03-10 ''' #窗口结构 class RECT(ctypes.Structure): _fields_ = [('left', ctypes.c_long), ('top', ctypes.c_long), ('right', ctypes.c_long), ('bottom', ctypes.c_long)] def __str__(self): return str((self.left, self.top, self.right, self.bottom)) rect = RECT() #获取当前窗口句柄 HWND = win32gui.GetForegroundWindow() #取当前窗口坐标 ctypes.windll.user32.GetWindowRect(HWND,ctypes.byref(rect)) #调整坐标 rangle = (rect.left+2,rect.top+2,rect.right-2,rect.bottom-2) #抓图 pic = ImageGrab.grab(rangle) #保存 save_pic(pic) def capture_choose_windows(): ''' Function:抓取选择的区域,没有自己写这个,借用QQ抓图功能 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-03-10 ''' try: #加载QQ抓图使用的dll dll_handle = ctypes.cdll.LoadLibrary('CameraDll.dll') except Exception: try: #如果dll加载失败,则换种方法使用,直接运行,如果还失败,退出 os.system("Rundll32.exe CameraDll.dll, CameraSubArea") except Exception: return else: try: #加载dll成功,则调用抓图函数,注:没有分析清楚这个函数带的参数个数 #及类型,所以此语句执行后会报参数缺少4个字节,但不影响抓图功能,所 #以直接忽略了些异常 dll_handle.CameraSubArea(0) except Exception: return def save_pic(pic, filename = '未命令图片.png'): ''' Function:使用文件对框,保存图片 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-03-10 ''' app = wx.PySimpleApp() wildcard = "PNG(*.png)|*.png" dialog = wx.FileDialog(None, "Select a place", os.getcwd(), filename, wildcard, wx.SAVE) if dialog.ShowModal() == wx.ID_OK: pic.save(dialog.GetPath().encode('gb2312')) else: pass dialog.Destroy() def main(): ''' Function:主函数,注册快捷键 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-03-10 ''' #创建hotkey句柄 hot_handle = pyhk.pyhk() #注册抓取全屏快捷键CTRL+F1 hot_handle.addHotkey(['Ctrl', 'F1'], capture_fullscreen) #注册抓取当前窗口快捷键CTRL+F2 hot_handle.addHotkey(['Ctrl', 'F2'], capture_current_windows) #注册抓取所选区域快捷键CTRL+F3 hot_handle.addHotkey(['Ctrl', 'F3'], capture_choose_windows) #开始运行 hot_handle.start() if __name__ == "__main__": main() ~~~ ### 二、测试: 程序运行后,可以看别的事情,如果要抓图,根据要抓的图片类型,按下相应的快捷键即可,以下图片除第一张外,其他均使用本程序抓区。 1、抓到后自动弹出保存对话框 ![](https://box.kancloud.cn/2016-06-08_5757935941563.gif) 2、抓全屏(CTRL+F1) ![](https://box.kancloud.cn/2016-06-08_57579359559d4.gif) 2、抓当前窗口(CTRL+F2) ![](https://box.kancloud.cn/2016-06-08_57579359722b4.gif) 3、抓所选区域(CTRL+F3) ![](https://box.kancloud.cn/2016-06-08_57579359940ba.gif) ### 三、有待改进: 后面考虑增加托盘显示、用户自定义快捷键等功能,最终以exe格式发布(目前只做到了windows下使用,Linux下有空再研究一下)