截图屏幕的几个区域,并使用PyAutoGui-Python3.4保存它们
我是python编程的初学者,我对PyAutoGui中的屏幕截图函数有问题 这是我的密码:截图屏幕的几个区域,并使用PyAutoGui-Python3.4保存它们,python,python-3.x,screenshot,python-3.4,pyautogui,Python,Python 3.x,Screenshot,Python 3.4,Pyautogui,我是python编程的初学者,我对PyAutoGui中的屏幕截图函数有问题 这是我的密码: #Libraries import pyautogui, os #Work Directory os.chdir('C:/Users/mypath') #Data and Variables: ListOfNames=['T1.png','T2.png','T3.png'] #list of desired positions to screenshot: Several_Regions=[(760
#Libraries
import pyautogui, os
#Work Directory
os.chdir('C:/Users/mypath')
#Data and Variables:
ListOfNames=['T1.png','T2.png','T3.png']
#list of desired positions to screenshot:
Several_Regions=[(760, 142, 22, 23),(692, 352, 19, 21),(553, 456, 19, 21)]
#Program:
for name in ListOfNames:
for LeftTopWidthHeight in Several_Regions:
pyautogui.screenshot('%s'%(name), region = LeftTopWidthHeight)
这是我提到的每个地区的截图
在区域列表中,三个.png文件中有几个区域
但是它创建了3.png文件,其中包含完全相同的区域
屏幕截图这是第三个(也是最后一个)区域(553456,19,21)
照片:
我忘在什么地方了吗?请帮我解决这个问题:)这是因为在嵌套的
循环中,您实际制作了九个屏幕截图。对于每个文件,保存第三个:最后一个区域。因为在嵌套for循环中,您将实际使用参数制作屏幕截图:
名称列表[0],几个区域[0]
名称列表[0],几个区域[1]
名称列表[0],几个区域[2]
名称列表[1],几个区域[0]
名称列表[1],几个地区[1]
名称列表[1],几个地区[2]
名称列表[2],几个区域[0]
名称列表[2],几个地区[1]
,以及
名称列表[2],几个区域[2]
如您所见,对于每个ListOfNames
,最后一个调用的是多个区域[2]
但是,您可以使用zip
确保将第一个区域保存到第一个文件名,等等:
for name,LeftTopWidthHeight in zip(ListOfNames,Several_Regions):
pyautogui.screenshot('%s'%(name), region = LeftTopWidthHeight)
for idx,LeftTopWidthHeight in enumerate(Several_Regions):
pyautogui.screenshot('T%s.png'%idx, region = LeftTopWidthHeight)