首页 > 代码库 > Python OCR提取普通数字图形验证中的数字
Python OCR提取普通数字图形验证中的数字
截取图形验证码:
1 # -*- coding: UTF-8 -*- 2 ‘‘‘ 3 Created on 2016年7月4日 4 5 @author: xuxianglin 6 ‘‘‘ 7 import os 8 import tempfile 9 import shutil10 11 from PIL import Image12 13 PATH = lambda p: os.path.abspath(p)14 TEMP_FILE = PATH(tempfile.gettempdir() + "/temp_screen.png")15 16 class Appium_Extend(object):17 def __init__(self, driver):18 self.driver = driver19 20 def get_screenshot_by_element(self, element):21 #先截取整个屏幕,存储至系统临时目录下22 self.driver.get_screenshot_as_file(TEMP_FILE)23 24 #获取元素bounds25 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 return self35 36 def get_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 return self46 47 def write_to_file( self, dirPath, imageName, form = "png"):48 #将截屏文件复制到指定目录下49 if not os.path.isdir(dirPath):50 os.makedirs(dirPath)51 shutil.copyfile(TEMP_FILE, PATH(dirPath + "/" + imageName + "." + form))52 53 def load_image(self, image_path):54 #加载目标图片供对比用55 if os.path.isfile(image_path):56 load = Image.open(image_path)57 return load58 else:59 raise Exception("%s is not exist" %image_path)60 61 def same_as(self, load_image, percent):62 #对比图片,percent值设为0,则100%相似时返回True,设置的值越大,相差越大63 import math64 import operator65 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 return True76 else:77 return False
在定位元素中调用改方法:
1 vcode=self.driver.find_element_by_id("com.ajb.sp:id/changepsw_code_img")2 self.extend.get_screenshot_by_element(vcode).write_to_file("..\\resource\\vcode\\","image")3 self.assertTrue(os.path.isfile(‘..\\resource\\vcode\\image.png‘))4 image=Image.open(‘..\\resource\\vcode\\image.png‘)5 vcode_number=pytesseract.image_to_string(image)6 print u"截取到的验证码为:%s"%vcode_number
Python OCR提取普通数字图形验证中的数字
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。