当前位置:编程学习 > 网站相关 >>

Python:一个多功能的抓图工具开发(附源码)

最近几年,各种论坛回帖中经常会见到这样的回复:无图无真相。还有经常写工作周报或者实验结果时,均要贴几张图上去。所以,抓图的需求在工作和生活中应用算是比较多的了,如果在家,我个人一直使用PrtSc键或QQ的抓图工具,用PrtScr键的一个问题是,我必须打开画图板(mspaint.exe),把剪贴板中的图片黏贴到画布上,再另存为,还是比较麻烦的。如果在公司,因为用不了QQ,此时,要抓个一块区域上的图,还要通过画图板来编辑一下,也是比较浪费时间的。

   为解决一上问题,今天使用python写了一个抓图的工具,支持使用快捷键抓取屏幕、当前窗口,所选区域三种需求,抓到之后并弹出保存对话框,再也不用使用画图板中转一下了,进入正题:

一、代码:


[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()     
补充:Web开发 , Python ,

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,