截取图形验证码:

 # -*- 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. mysql存储引擎之myisam学习

    myisam存储引擎特点:1.不支持事务2.表级锁定(更新时锁整个表,其索引机制是表级索引,这虽然可以让锁定的实现成本很小,但是也同时大大降低 了其并发性能) 3.读写互相阻塞:不仅会在写入的时候阻塞 ...

  2. springMvc架构简介

    什么是spring 关于spring的定义无论是从官方还是市面上已经很多能够清晰明了的做出解释了.我姑且简单定义它为一个轻量级的控制反转(IoC)和面向切面(AOP)的容器,Java 开发框架,至于控 ...

  3. OpenSSL生成公钥私钥***

    证书标准 X.509 - 这是一种证书标准,主要定义了证书中应该包含哪些内容.其详情可以参考RFC5280,SSL使用的就是这种证书标准. 编码格式 同样的X.509证书,可能有不同的编码格式,目前有 ...

  4. Jenkins的项目管理

    新建Item 使用Jenkins最重要的是能够创建一些工作流,除了部署,还能做很多流程上的事情.同样,一条条项目建起来需要做一定的管理,在Jenkins首页Jenkins->新建可以按自己的需要 ...

  5. Python中的Bunch模式

    引用: 当树这样的数据结构被原型化(或者乃至于被定型)时,它往往会时一个非常有用而灵活的类型,允许我们在其构造器中设置任何属性.在这些情况下,我们会需要用到一种叫做“Bunch”的设计模式. clas ...

  6. 第十章 Secret & Configmap (中)

    10.3 在Pod中使用Secret 10.3.1 Volume方式 apiVersion: v1 kind: Pod metaata: name: mypod spec: containers: - ...

  7. 设计模式—单例模式(java)

    一:懒汉式 1:  线程安全的双重锁检查机制 public class Singleton{ private Singleton() {}    //  私有构造函数,保证不被外界实例化(不考虑反射) ...

  8. 示例文件下载demo

    页面: 后台: package com.js.ai.modules.shwindow.util; import java.io.BufferedInputStream; import java.io. ...

  9. 用户从手机的浏览器访问www.baidu.com,看到的可能跟桌面PC电脑,是不太一样的网页效果,会更适合移动设备使用。请简要分析一下,实现这种网页区分显示的原因及技术原理。

    手机的网速问题.屏幕大小.内存.CPU等.通过不同设备的特征,实现不同的网页展现或输出效果.根据useragent.屏幕大小信息.IP.网速.css media Query等原理,实现前端或后端的特征 ...

  10. nginx 1.12 配置解析php

    server { listen 80; server_name foo.com; root /path; index index.html index.htm index.php; location ...