MonkeyRunner读取本机图片进行对比loadImageFromFile

MonkeyRunner读取本机图片进行对比loadImageFromFile

上一篇文章介绍了使用monkeyrunner截取指定区域的图像并进行对比,日常使用中大多数时候需要读取本机的图片和monkeyrunner的截图进行对比。

MonkeyRunner.loadImageFromFile(‘./shot.png’)方法可以实现读取本地图片供MonkeyRunner使用的功能,参数是本地图片路径。需要特别注意loadImageFromFile的大小写,否则会出现AttributeError的提示。

MonkeyRunner读取本机图片进行对比,示例如下:
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
device = MonkeyRunner.waitForConnection(5,’emulator-5554′)
device.startActivity(component=’com.estrongs.android.pop/.view.FileExplorerActivity’)

#读取本地文件夹的图片,用作图片对比
pic=MonkeyRunner.loadImageFromFile(‘./shot.png’)

#截图,整个屏幕
newpic=device.takeSnapshot()
#获取坐标点指定区域的图像
newpic=newpic.getSubImage((0,57,715,1123))

#和电脑上保存的图片进行对比,并输出结果
print (newpic.sameAs(pic,1.0))

newpic.writeToFile(‘./shot1.png’,’png’)

6san.com

发表评论