python - PyGetWindow 可以帮忙截图吗?
问题描述
我可以使用PyGetWindow帮助 pyautogui 截取特定窗口吗?如果是,如何?谢谢。
解决方案
是的,您可以制作我正在使用新窗口的特定窗口的屏幕截图,并考虑到您已经考虑了将光标移动到此示例的颜色。
import os
import pygetwindow
import pyautogui
z1 = pygetwindow.getAllTitles()
time.sleep(1)
# open a file
os.startfile("C:\\Users\\name\\path")
time.sleep(1)
z2 = pygetwindow.getAllTitles()
# gets new window title
z3 = [x for x in z2 if x not in z1]
z3 = ''.join(z3)
# activate new window
y = pygetwindow.getWindowsWithTitle(z3)[0]
y.activate()
color_locate = (220,220,220) # edit the color here
locations_of_color = []
s = pyautogui.screenshot()
s.save(r'C:\\Users\\user\\Pictures\\folder\\s.png')
for xx in range(s.width):
for yy in range(s.height):
if s.getpixel((xx, yy)) == color_locate:
located_color = pyautogui.position(xx, yy)
a = [i for i in located_color]
locations_of_color.append(a)
print(len(locations_of_color))
time.sleep(3)
# tries to move to middle
try:
i = len(locations_of_color) // 2
pyautogui.moveTo(locations_of_color[i])
except:
try:
pyautogui.moveTo(locations_of_color[0])
except:
pyautogui.moveTo(locations_of_color)
time.sleep(3)
如果您只想要屏幕截图,请使用顶部代码(直到 y.activate()),然后执行以下操作:
s = pyautogui.screenshot()
s.save(r'C:\\Users\\user\\Pictures\\folder\\s.png')