---
title: "MonkeyRunner读取本机图片进行对比loadImageFromFile"
author: "wait_miracle"
date: "2014-02-08T17:02:22Z"
tags: []
categories: ["移动开发"]
slug: "monkeyrunner-loadimagefromfile"
draft: false
lastmod: "2014-02-08T17:02:22Z"
id: 2012455
url: "https://wait-miracle.iteye.com/blog/2012455"
---

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’)

**转自：** [http://www.6san.com/908/](http://www.6san.com/908/ "MonkeyRunner读取本机图片进行对比loadImageFromFile")