截取图形验证码:

 # -*- 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提取普通数字图形验证中的数字的更多相关文章

  1. PYTHON 写函数,计算传入字符串中【数字、字母、空格、以及其他的个数】

    def func1(s): al_num = 0 spance_num = 0 digit_num = 0 others_num = 0 for i in s: if i.isdigit(): # i ...

  2. python正则表达式提取字符串

    用python正则表达式提取字符串 在日常工作中经常遇见在文本中提取特定位置字符串的需求.python的正则性能好,很适合做这类字符串的提取,这里讲一下提取的技巧,正则表达式的基础知识就不说了,有兴趣 ...

  3. python(15)提取字符串中的数字

    python 提取一段字符串中去数字 ss = “123ab45” 方法一:filter filter(str.isdigit, ss) 别处copy的filter的用法: # one>> ...

  4. python 提取字符串中的数字组成新的字符串

    方法一 # 有一个字符串text = "aAsmr3idd4bgs7Dlsf9eAF" # 请将text字符串中的数字取出,并输出成一个新的字符串 import re text = ...

  5. 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) ...

  6. C++ 提取字符串中的数字

    C++ 提取字符串中的数字 #include <iostream> using namespace std; int main() { ] = "1ab2cd3ef45g&quo ...

  7. 8.python中的数字

    python中数字对象的创建如下, a = 123 b = 1.23 c = 1+1j 可以直接输入数字,然后赋值给变量. 同样也可是使用类的方式: a = int(123) b = float(1. ...

  8. php提取字符串中的数字

    最近工作中写代码的时候需要在一串字符串中将所有的数字提取出来这么一个小功能,研究了一下发现方法还挺多,值得记录一下,于是对如何使用PHP将字符串中的数字提取出来的功能做了一个小总结,总结三种方法如下: ...

  9. delphi 提取字符中的数字

    Function Setstring(cString:string):string;   {提取数字} VAr   i:integer;   str:string;  begin    str:='' ...

随机推荐

  1. SAX解析类:SaxHelper

    public class SaxHelper extends DefaultHandler { private Person person; private ArrayList<Person&g ...

  2. 【洛谷】P2983 [USACO10FEB]购买巧克力Chocolate Buying(贪心)

    题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...

  3. oracle系统表v$session、v$sql字段说明(转)

    在本视图中,每一个连接到数据库实例中的 session都拥有一条记录.包括用户 session及后台进程如 DBWR, LGWR, arcchiver等等. V$SESSION中的常用列 V$SESS ...

  4. PHP处理session跨域

    同一根域名下子域名之间的跨域 ini_set('session.name', 'sid'); //设置session_id的键名 ini_set('session.use_trans_sid', 0) ...

  5. mysql和php的内存问题

    最近给一个客户把他以前的数据转换到PHPSMC里去,因为他的数据太大出现一些问题,首先出现的是php内存问题,刚导时空白,我就在该也面的头部加上 error_reporting(E_ALL);打印所有 ...

  6. Spring Boot实践——三种拦截器的创建

    引用:https://blog.csdn.net/hongxingxiaonan/article/details/48090075 Spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可 ...

  7. 拼接两个yuv合帧

    http://blog.csdn.net/huahuahailang/article/details/9040847 /**************************************** ...

  8. python:随机数 random

    #随机数 import random print(random.randint(10,12))#生成10-12之间的整数 print(random.uniform(10,12))#生成10-12之间的 ...

  9. Thymeleaf 条件语句

    Thymeleaf 条件语句  Posted on 2017-09-15 if 与 unless 假如现在有一个商品列表,当商品有评论时,显示view按钮,否则不显示.这时候就可以使用Thymelea ...

  10. ASP.NET Web API(C#)学习01

    Web Api 记得去年公司有个分享会分享了这个,最近留意招聘信息的时候,发现有个招聘信息的要求是会用WebApi,然后花了半个小时不到,根据下面这篇文章了解了一下,觉得这个东西也不难啊. 突然发现在 ...