python基于opencv批量生成验证码的示例


Posted in Python onApril 28, 2021

基本思路是使用opencv来把随机生成的字符,和随机生成的线段,放到一个随机生成的图像中去。

  虽然没有加复杂的形态学处理,但是目前看起来效果还不错

  尝试生成1000张图片,但是最后只有998张,因为有有重复的,被覆盖掉了。

  代码如下:

import cv2
import numpy as np
line_num = 10
pic_num = 1000
path = "./imgs/"
def randcolor():        
    return (np.random.randint(0,255),np.random.randint(0,255),np.random.randint(0,255))
    
def randchar():
    return chr(np.random.randint(65,90))
    
def randpos(x_start,x_end,y_start,y_end):
    return (np.random.randint(x_start,x_end),
            np.random.randint(y_start,y_end))
    
    
img_heigth = 60
img_width = 240
for i in range(pic_num):
    img_name = ""
    #生成一个随机矩阵,randint(low[, high, size, dtype])
    img = np.random.randint(100,200,(img_heigth,img_width, 3), np.uint8)
    #显示图像
    #cv2.imshow("ranImg",img)
    
    x_pos = 0
    y_pos = 25
    for i in range(4):
        char = randchar()
        img_name += char
        cv2.putText(img,char,
                    (np.random.randint(x_pos,x_pos + 50),np.random.randint(y_pos,y_pos + 35)), 
                    cv2.FONT_HERSHEY_SIMPLEX,
                    1.5,
                    randcolor(),
                    2,
                    cv2.LINE_AA)
        x_pos += 45
    
    #cv2.imshow("res",img)
    
    #添加线段
    for i in range(line_num):
        img = cv2.line(img,
                       randpos(0,img_width,0,img_heigth),
                       randpos(0,img_width,0,img_heigth),
                        randcolor(),
                        np.random.randint(1,2))
        
    #cv2.imshow("line",img)
    cv2.imwrite(path + img_name + ".jpg",img)
    #cv2.waitKey(0)                  
    #cv2.destroyAllWindows()

  结果:

python基于opencv批量生成验证码的示例

以上就是python基于opencv批量生成验证码的示例的详细内容,更多关于python 批量生成验证码的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python中实现结构相似的函数调用方法
Mar 10 Python
Python常用的内置序列结构(列表、元组、字典)学习笔记
Jul 08 Python
Python 登录网站详解及实例
Apr 11 Python
Python3中的列表,元组,字典,字符串相关知识小结
Nov 10 Python
Python排序搜索基本算法之堆排序实例详解
Dec 08 Python
解决python3中cv2读取中文路径的问题
Dec 05 Python
详解Python学习之安装pandas
Apr 16 Python
Python学习笔记之Zip和Enumerate用法实例分析
Aug 14 Python
在Python中获取操作系统的进程信息
Aug 27 Python
使用Django搭建一个基金模拟交易系统教程
Nov 18 Python
Python 类的魔法属性用法实例分析
Nov 21 Python
selenium+python实现自动登陆QQ邮箱并发送邮件功能
Dec 13 Python
python基于tkinter制作下班倒计时工具
Apr 28 #Python
Python爬虫之爬取哔哩哔哩热门视频排行榜
k-means & DBSCAN 总结
秀!学妹看见都惊呆的Python小招数!【详细语言特性使用技巧】
Apr 27 #Python
Python代码,能玩30多款童年游戏!这些有几个是你玩过的
python实现腾讯滑块验证码识别
Apr 27 #Python
python实现调用摄像头并拍照发邮箱
Apr 27 #Python
You might like
使用PHP和XSL stylesheets转换XML文档
2006/10/09 PHP
dedecms系统常用术语汇总
2007/04/03 PHP
libmysql.dll与php.ini是否真的要拷贝到c:\windows目录下呢
2010/03/15 PHP
奉献出一个封装的curl函数 便于调用(抓数据专用)
2013/07/22 PHP
浅析php数据类型转换
2014/01/09 PHP
php CI框架插入一条或多条sql记录示例
2014/07/29 PHP
浅谈php+phpStorm+xdebug配置方法
2015/09/17 PHP
PHP验证码类ValidateCode解析
2017/01/07 PHP
cookie在javascript中的使用技巧以及隐私在服务器端的设置
2012/12/03 Javascript
浅析jquery某一元素重复绑定的问题
2014/01/03 Javascript
js中日期的加减法
2015/05/06 Javascript
理解jquery事件冒泡
2016/01/03 Javascript
Angular ng-repeat指令实例以及扩展部分
2016/12/26 Javascript
jQuery实用密码强度检测
2017/03/02 Javascript
AngularJS 仿微信图片手势缩放的实例
2017/09/28 Javascript
vue中路由参数传递可能会遇到的坑
2017/12/07 Javascript
vue.js单文件组件中非父子组件的传值实例
2018/09/13 Javascript
原生JavaScript实现贪吃蛇游戏
2020/11/04 Javascript
[02:53]DOTA2亚洲邀请赛 NewBee战队巡礼
2015/02/03 DOTA
Python 爬虫学习笔记之单线程爬虫
2016/09/21 Python
ansible作为python模块库使用的方法实例
2017/01/17 Python
用python 批量更改图像尺寸到统一大小的方法
2018/03/31 Python
Python3非对称加密算法RSA实例详解
2018/12/06 Python
Python 实现一个手机号码获取妹子名字的功能
2019/09/25 Python
python将三维数组展开成二维数组的实现
2019/11/30 Python
Python内置类型性能分析过程实例
2020/01/29 Python
opencv 图像加法与图像融合的实现代码
2020/07/08 Python
Python如何实现大型数组运算(使用NumPy)
2020/07/24 Python
澳大利亚家庭花园和DIY工具网店:VidaXL
2019/05/03 全球购物
Hello Molly美国:女性时尚在线
2019/08/26 全球购物
电大物流学生的自我评价
2013/10/25 职场文书
就业协议书
2014/09/12 职场文书
如何使JavaScript休眠或等待
2021/04/27 Javascript
分布式Redis Cluster集群搭建与Redis基本用法
2022/02/24 Redis
Win11 引入 Windows 365 云操作系统,适应疫情期间混合办公模式:启动时直接登录、模
2022/04/06 数码科技
科学家研发出新型速效酶,可在 24 小时内降解塑料制品
2022/04/29 数码科技