没有 PIL 的 Python windows 7 屏幕截图

2022-01-18 00:00:00 python image screenshot windows-7

问题描述

我想用python截图.

I want to take a screenshot using python.

我曾尝试使用 PIL,但由于我使用的是 64 位 Windows 并且 python PIL 不起作用(我只能找到 32 位 PIL 版本).顺便说一句,我使用的是 python 2.7.1.

I have tried using PIL, but since I am using 64bit windows and python PIL does not work (I could only find 32bit PIL versions). I am using python 2.7.1 by the way.

我要截个图,怎么做都无所谓,只要能达到每秒1个以上的速度即可.最好它还应该能够裁剪截取屏幕截图的区域,但这并不是最重要的.

I want to take a screenshot, it doesn't really matter how, as long as it can take more than 1 per second in speed. Preferably it should also be able to crop the area it takes a screenshot of, but that's not of the utmost importance.

主要问题似乎是我在 64 位上运行,而且很多事情似乎与此不兼容.如果可能的话,我真的不想回到 32 位.是否有任何程序或模块可以做到这一点?

The main problem seems to be I'm running on 64bit and a lot of things seem incompatible with that. I don't really want to move back to 32bit though if at all possible. Are there any programs or modules that can do this?


解决方案

在 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil.

from PIL import ImageGrab
im = ImageGrab.grab()
im.save('screenshot.png')

更新:改用 pywin32 (http://sourceforge.net/projects/pywin32/)PIL 对多个虚拟屏幕进行截图:

Update: use pywin32 (http://sourceforge.net/projects/pywin32/) instead of PIL to take screenshots of multiple virtual screens:

import win32gui, win32ui, win32con, win32api
hwin = win32gui.GetDesktopWindow()
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
hwindc = win32gui.GetWindowDC(hwin)
srcdc = win32ui.CreateDCFromHandle(hwindc)
memdc = srcdc.CreateCompatibleDC()
bmp = win32ui.CreateBitmap()
bmp.CreateCompatibleBitmap(srcdc, width, height)
memdc.SelectObject(bmp)
memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
bmp.SaveBitmapFile(memdc, 'screenshot.bmp')

相关文章