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:='' ...
随机推荐
- RK3288 OTG切换为Host模式
OTG = On The Go,是USB的一种工作模式 USB协议中规定了两类设备,一类是Host.一类是Device. Host为主控设备,如PC,Device为从设备,如鼠标.U盘等,Device ...
- python is 和 == 的区别
一.is 和 == 的区别 == 比较 比较的俩边的值 is 比较 比较的是内存地址 id() 二.小数据池 数字小数据池的范围 -5 ~ 256 字符串中如果有特殊字符他们的内存地址就不一样 字符串 ...
- mybatis实现继承映射
ORM 框架的优势在于能让我们利用面向对象的思维去操作数据库, hibernate 作为重量级的 ORM 框架对面向对象的支持很强大.作为半自动化的 mybatis ,对面向对象的支持也是很完备的.这 ...
- 将子类对象引用赋值给超类对象 JAVA 编译时多态性
将子类对象引用赋值给超类对象 JAVA 编译时多态性(转) (2012-05-10 11:24:05) 转载▼ 标签: 杂谈 分类: 也无晴_soft 1.通过将子类对象引用赋值给超类对象引用变量来实 ...
- Julia - 三元运算符
三元运算符的格式: a ? b : c a 是条件表达式,如果条件 a 为真,就执行 b:如果条件 a 为假,就执行 c 二选一 julia> println(1 < 2 ? " ...
- Tkinter Fonts(字体)
Python GUI - Tkinter Fonts:作为一个tuple的第一个元素是字体家族,一个点的大小,可选择一个字符串,包含一个或更多的粗体,斜体,下划线的样式修饰符,加粗. 最多可能有三 ...
- C++何时使用引用
- K老在拿图灵奖时的发言:Computer Programming as an Art
很多话说得很透彻,把一些觉比较精彩的摘抄一下. ... It seems to me that if the authors I studied were writing today, they wo ...
- 数据分析之Numpy-数组计算
引言 : 数据分析 : 就是把隐藏在一些看似杂乱无章的数据背后的信息提炼出来,总结出研究对象的内在规律 . 数据分析三剑客 : Numpy 数组计算 Pandas 表计算与数据分析 ...
- 使用Spring MVC创建 REST API--2
1.提供资源之外的其他内容 @ResponseBody提供了一种很有用的方式,能够将控制器返回的Java对象转换为发送到客户端的资源表述.实际上,将资源表述发送给客户端只是整个过程的一部分.一个好的R ...