600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > python抓取图片数字_Python OCR提取普通数字图形验证中的数字

python抓取图片数字_Python OCR提取普通数字图形验证中的数字

时间:2022-08-26 20:18:26

相关推荐

python抓取图片数字_Python OCR提取普通数字图形验证中的数字

1 #-*- coding: UTF-8 -*-

2 '''

3 Created on 7月4日4

5 @author: xuxianglin6 '''

7 importos8 importtempfile9 importshutil10

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

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

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。