Python OCR提取普通数字图形验证中的数字
截取图形验证码:
# -*- coding: UTF-8 -*-
'''
Created on 2016年7月4日 @author: xuxianglin
'''
import os
import tempfile
import shutil from PIL import Image PATH = lambda p: os.path.abspath(p)
TEMP_FILE = PATH(tempfile.gettempdir() + "/temp_screen.png") class Appium_Extend(object):
def __init__(self, driver):
self.driver = driver def get_screenshot_by_element(self, element):
#先截取整个屏幕,存储至系统临时目录下
self.driver.get_screenshot_as_file(TEMP_FILE) #获取元素bounds
location = element.location
size = element.size
box = (location["x"], location["y"], location["x"] + size["width"], location["y"] + size["height"]) #截取图片
image = Image.open(TEMP_FILE)
newImage = image.crop(box)
newImage.save(TEMP_FILE) return self def get_screenshot_by_custom_size(self, start_x, start_y, end_x, end_y):
#自定义截取范围
self.driver.get_screenshot_as_file(TEMP_FILE)
box = (start_x, start_y, end_x, end_y) image = Image.open(TEMP_FILE)
newImage = image.crop(box)
newImage.save(TEMP_FILE) return self def write_to_file( self, dirPath, imageName, form = "png"):
#将截屏文件复制到指定目录下
if not os.path.isdir(dirPath):
os.makedirs(dirPath)
shutil.copyfile(TEMP_FILE, PATH(dirPath + "/" + imageName + "." + form)) def load_image(self, image_path):
#加载目标图片供对比用
if os.path.isfile(image_path):
load = Image.open(image_path)
return load
else:
raise Exception("%s is not exist" %image_path) def same_as(self, load_image, percent):
#对比图片,percent值设为0,则100%相似时返回True,设置的值越大,相差越大
import math
import operator image1 = Image.open(TEMP_FILE)
image2 = load_image histogram1 = image1.histogram()
histogram2 = image2.histogram() differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2, \
histogram1, histogram2)))/len(histogram1))
if differ <= percent:
return True
else:
return False
在定位元素中调用改方法:
vcode=self.driver.find_element_by_id("com.ajb.sp:id/changepsw_code_img")
self.extend.get_screenshot_by_element(vcode).write_to_file("..\\resource\\vcode\\","image")
self.assertTrue(os.path.isfile('..\\resource\\vcode\\image.png'))
image=Image.open('..\\resource\\vcode\\image.png')
vcode_number=pytesseract.image_to_string(image)
print u"截取到的验证码为:%s"%vcode_number
Python OCR提取普通数字图形验证中的数字的更多相关文章
- PYTHON 写函数,计算传入字符串中【数字、字母、空格、以及其他的个数】
def func1(s): al_num = 0 spance_num = 0 digit_num = 0 others_num = 0 for i in s: if i.isdigit(): # i ...
- python正则表达式提取字符串
用python正则表达式提取字符串 在日常工作中经常遇见在文本中提取特定位置字符串的需求.python的正则性能好,很适合做这类字符串的提取,这里讲一下提取的技巧,正则表达式的基础知识就不说了,有兴趣 ...
- python(15)提取字符串中的数字
python 提取一段字符串中去数字 ss = “123ab45” 方法一:filter filter(str.isdigit, ss) 别处copy的filter的用法: # one>> ...
- python 提取字符串中的数字组成新的字符串
方法一 # 有一个字符串text = "aAsmr3idd4bgs7Dlsf9eAF" # 请将text字符串中的数字取出,并输出成一个新的字符串 import re text = ...
- Excel中如何提取字符串中的数字
取字符串中的数字,假如数据在A列,提取公式为 =LOOKUP(9^9,--MID(A1,MIN(FIND({1,2,3,4,5,6,7,8,9,0},A1&5^19)),ROW($1:$99) ...
- C++ 提取字符串中的数字
C++ 提取字符串中的数字 #include <iostream> using namespace std; int main() { ] = "1ab2cd3ef45g&quo ...
- 8.python中的数字
python中数字对象的创建如下, a = 123 b = 1.23 c = 1+1j 可以直接输入数字,然后赋值给变量. 同样也可是使用类的方式: a = int(123) b = float(1. ...
- php提取字符串中的数字
最近工作中写代码的时候需要在一串字符串中将所有的数字提取出来这么一个小功能,研究了一下发现方法还挺多,值得记录一下,于是对如何使用PHP将字符串中的数字提取出来的功能做了一个小总结,总结三种方法如下: ...
- delphi 提取字符中的数字
Function Setstring(cString:string):string; {提取数字} VAr i:integer; str:string; begin str:='' ...
随机推荐
- mysql的partition分区
前言:当一个表里面存储的数据特别多的时候,比如单个.myd数据都已经达到10G了的话,必然导致读取的效率很低,这个时候我们可以采用把数据分到几张表里面来解决问题.方式一:通过业务逻辑根据数据的大小通过 ...
- curl post数据
调用方式: $header = self::getHeader(); $data = self::postUrl($url, $header); /** * 组合Header * @return ty ...
- net start mongodb发生系统错误2 系统找不到指定的文件
安装mongodb时, 将mongodb 作为系统服务启动 net start mongodb,报错发生系统错误2 系统找不到指定的文件 . 查找原因是因为,系统服务的可执行文件地址有误. 修改服务地 ...
- 历届试题 Excel地址
问题描述 Excel单元格的地址表示很有趣,它使用字母来表示列号. 比如, A表示第1列, B表示第2列, Z表示第26列, AA表示第27列, AB表示第28列, BA表示第53列, .... 当然 ...
- rtmp发送H264及aac的音视频
RTMP推送的音视频流的封装形式和FLV格式相似,由此可知,向FMS推送H264和AAC直播流,需要首先发送"AVC sequence header"和"AAC sequ ...
- 让输入的字符转义成html实体的方法
使用 htmlspecialchars() 函数,代码不会执行,因为会被保存为转义代码 总结测试方法: https://www.cnblogs.com/kaibindirver/p/10321448. ...
- TCP之三:TCP/IP协议中backlog参数(队列参数)
目录: <TCP洪水攻击(SYN Flood)的诊断和处理> <TCP/IP协议中backlog参数> TCP建立连接是要进行三次握手,但是否完成三次握手后,服务器就处理(ac ...
- python算两个时间之间的天数,将天数转成int型
import time import datetime #计算两个日期相差天数,自定义函数名,和两个日期的变量名. def Caltime(date1,date2): #%Y-%m-%d为日期格式,其 ...
- canvas之抒写文字
<canvas id="canvas" width="500" height="400" style="background ...
- jQuery笔记——UI
jQuery UI 的官网网站为:http://jqueryui.com/,我们下载最新版本的即可,使用JQueryUI中的样式比我们使用原生的HTML要好看,还会有一些封装好的特效,JQueryUI ...