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

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

2 '''

3 Created on 2016年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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值