Chinaunix

标题: Python窗口截图,PIL方式,非win32gui [打印本页]

作者: 我不是老手    时间: 2009-12-02 18:18
标题: Python窗口截图,PIL方式,非win32gui
# -*- coding: cp936 -*-
import os
import ctypes
from PIL import ImageGrab
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))
def capture(hwnd,tempdir):
    """
    对窗口截图,传入窗口句柄及保存位置,生成的文件为temp.jpg
    """
    rect = RECT()
    ctypes.windll.user32.GetWindowRect(ieHWND,ctypes.byref(rect))
    rangle = (rect.left+2,rect.top+2,rect.right-2,rect.bottom-2)
    im = ImageGrab.grab(rangle)
    jpgname = "temp.jpg"
    Xini1 = os.path.join(tempdir,jpgname)
    Xini111 = Xini1.replace("\\","/")
    im.save(Xini111)  
    return
def test1():
    ieHWND = ctypes.windll.user32.FindWindowA("IEFrame",None)#获取IE的句柄
    getdir = os.getcwd()
    HWND_TOP = 0
    SWP_NOMOVE= 2
    SWP_NOSIZE= 1
    SWP_SHOWWINDOW= 64
    ctypes.windll.user32.SetWindowPos(ieHWND, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW)
    import time
    time.sleep(5)#延迟5秒
    capture(ieHWND,getdir)
    return
   
if __name__ == "__main__":
    print "以IE窗口为测试窗口请先开一个IE然后再运行"
    test1()

[ 本帖最后由 我不是老手 于 2009-12-2 18:26 编辑 ]
作者: openspace    时间: 2009-12-02 18:47
这个除了要PIL库还需要别的么
刚试了一下

  1. 以IE窗口为测试窗口请先开一个IE然后再运行
  2. Traceback (most recent call last):
  3.   File "D:\Eclipse-3.3\workspace\Q&A\src\grap_screen.py", line 43, in <module>
  4.     test1()
  5.   File "D:\Eclipse-3.3\workspace\Q&A\src\grap_screen.py", line 38, in test1
  6.     capture(ieHWND,getdir)
  7.   File "D:\Eclipse-3.3\workspace\Q&A\src\grap_screen.py", line 19, in capture
  8.     ctypes.windll.user32.GetWindowRect(ieHWND,ctypes.byref(rect))
  9. NameError: global name 'ieHWND' is not defined
复制代码

作者: zzcclp    时间: 2009-12-03 13:38
ctypes.windll.user32.GetWindowRect(ieHWND,ctypes.byref(rect))
这句的参数应该换为:hwnd, ctypes.byref(rect)
你试试
作者: openspace    时间: 2009-12-03 13:46
标题: 回复 #3 zzcclp 的帖子
试了一下,出现了新的错误

  1. 以IE窗口为测试窗口请先开一个IE然后再运行
  2. Traceback (most recent call last):
  3.   File "D:\Eclipse-3.3\workspace\Q&A\src\grap_screen.py", line 43, in <module>
  4.     test1()
  5.   File "D:\Eclipse-3.3\workspace\Q&A\src\grap_screen.py", line 38, in test1
  6.     capture(ieHWND,getdir)
  7.   File "D:\Eclipse-3.3\workspace\Q&A\src\grap_screen.py", line 25, in capture
  8.     im.save(Xini111)  
  9.   File "E:\Python Toolkit\Python-2.6\Lib\site-packages\PIL\Image.py", line 1405, in save
  10.     save_handler(self, fp, filename)
  11.   File "E:\Python Toolkit\Python-2.6\Lib\site-packages\PIL\JpegImagePlugin.py", line 409, in _save
  12.     ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
  13.   File "E:\Python Toolkit\Python-2.6\Lib\site-packages\PIL\ImageFile.py", line 490, in _save
  14.     e.setimage(im.im, b)
  15. SystemError: tile cannot extend outside image

复制代码

作者: liberalwu    时间: 2009-12-06 21:24
标题: 回复 #1 我不是老手 的帖子
要将
ieHWND = ctypes.windll.user32.FindWindowA("IEFrame",None)#获取IE的句柄
这个要放到全局变量,不然ieHWND只作用在函数def test1()区域中~
capture函数无法获取变量值~




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2