截取图形验证码:

 # -*- 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. RK3288 usb 摄像头旋转

    系统:Android 5.1 下面实现了摄像头 180 度旋转,旋转角度只需修改 orientation. diff --git a/hardware/rockchip/camera/CameraHa ...

  2. FPGA之外,了解一下中断

    中断是什么? 中断的汉语解释是半中间发生阻隔.停顿或故障而断开.那么,在计算机系统中,我们为什么需要“阻隔.停顿和断开”呢? 举个日常生活中的例子,比如说我正在厨房用煤气烧一壶水,这样就只能守在厨房里 ...

  3. 关于git rebase的相关讲解

    http://gitbook.liuhui998.com/4_2.html 一.基本 git rebase用于把一个分支的修改合并到当前分支. 假设你现在基于远程分支"origin" ...

  4. 《Hadoop技术内幕》读书笔记——Task运行过程分析

    本文是董西成的Hadoop技术内幕一书的读书章节总结. 第八章 Task运行过程分析 所有Task需要周期性地向TaskTracker汇报最新进度和计数器值,而这正是由Reporter组件实现的,其中 ...

  5. 安装windows 2003 server

    最近去给客户安装windows 2003 server,想想也是技术,就写出来了,我这个实在虚拟机安装的,安装这个系统主要是为了安装limesurvey问卷系统 第一步:安装,选择Enter 第二步: ...

  6. 003:MySQL账号创建授权以及Workbench

    目录 一. 权限管理 1."用户 + IP"的概念 2. 用户权限管理 3. 基本操作 4. 撤销权限 5.授权和创建用户 二. MySQL模拟角色 三. Workbench与Ut ...

  7. 十八 线程暂停 suspend/ resume

    1  Suspend.resume 的缺点1 :独占!  线程执行到同步块中,如果线程暂停了,不会释放锁. 比如,比如System.out.println()方法就是一个同步方法, 如果线程调用Sys ...

  8. 【UVALive】3029 City Game(悬线法)

    题目 传送门:QWQ 分析 以前见到过差不多的这题. xhk说是单调栈水题,但我又不会单调栈,于是当时就放下了. 这么久过去了我还是不会用单调栈做这题,用的是悬线法. 非常好写 代码 #include ...

  9. Android:不同drawable文件夹的区别

    4.0后,新建android工程,会自动生成drawable,drawalbe-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi,drawable-xxh ...

  10. php header 302重定向失效问题

    E:\html\pim\php_weili_activities\application\controllers\user.php public function login() { if ($thi ...