1 #-*- coding: UTF-8 -*-
2 '''
3 Created on 2016年7月4日4
5 @author: xuxianglin6 '''
7 importos8 importtempfile9 importshutil10
python圖形界面?11 from PIL importImage12
13 PATH = lambdap: os.path.abspath(p)14 TEMP_FILE = PATH(tempfile.gettempdir() + "/temp_screen.png")15
16 classAppium_Extend(object):17 def __init__(self, driver):18 self.driver =driver19
20 defget_screenshot_by_element(self, element):21 #先截取整個屏幕,存儲至系統臨時目錄下
22 self.driver.get_screenshot_as_file(TEMP_FILE)23
24 #獲取元素bounds
python 圖形。25 location =element.location26 size =element.size27 box = (location["x"], location["y"], location["x"] + size["width"], location["y"] + size["height"])28
29 #截取圖片
30 image =Image.open(TEMP_FILE)31 newImage =image.crop(box)32 newImage.save(TEMP_FILE)33
34 returnself35
36 defget_screenshot_by_custom_size(self, start_x, start_y, end_x, end_y):37 #自定義截取范圍
38 self.driver.get_screenshot_as_file(TEMP_FILE)39 box =(start_x, start_y, end_x, end_y)40
python爬取數據。41 image =Image.open(TEMP_FILE)42 newImage =image.crop(box)43 newImage.save(TEMP_FILE)44
45 returnself46
47 def write_to_file( self, dirPath, imageName, form = "png"):48 #將截屏文件復制到指定目錄下
49 if notos.path.isdir(dirPath):50 os.makedirs(dirPath)51 shutil.copyfile(TEMP_FILE, PATH(dirPath + "/" + imageName + "." +form))52
53 defload_image(self, image_path):54 #加載目標圖片供對比用
55 ifos.path.isfile(image_path):56 load =Image.open(image_path)57 returnload58 else:59 raise Exception("%s is not exist" %image_path)60
python爬蟲教程?61 defsame_as(self, load_image, percent):62 #對比圖片,percent值設為0,則100%相似時返回True,設置的值越大,相差越大
63 importmath64 importoperator65
66 image1 =Image.open(TEMP_FILE)67 image2 =load_image68
69 histogram1 =image1.histogram()70 histogram2 =image2.histogram()71
72 differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2, \73 histogram1, histogram2)))/len(histogram1))74 if differ <=percent:75 returnTrue76 else:77 return False
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态