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中实现单例模式的三种方法
Apr 08 Python
简单谈谈python中的Queue与多进程
Aug 25 Python
Python基于Socket实现的简单聊天程序示例
Aug 05 Python
详解Python中 sys.argv[]的用法简明解释
Dec 20 Python
几行Python代码爬取3000+上市公司的信息
Jan 24 Python
解决Django生产环境无法加载静态文件问题的解决
Apr 23 Python
python 浅谈serial与stm32通信的编码问题
Dec 18 Python
Python 3 使用Pillow生成漂亮的分形树图片
Dec 24 Python
python实现俄罗斯方块游戏(改进版)
Mar 13 Python
python Pexpect模块的使用
Dec 25 Python
jupyter notebook保存文件默认路径更改方法汇总(亲测可以)
Jun 09 Python
PyQt5实现多张图片显示并滚动
Jun 11 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的MySQL连接类
2013/06/07 PHP
php中使用gd库实现远程图片下载实例
2015/05/12 PHP
php微信公众号开发之关键词回复
2018/10/20 PHP
thinkphp框架无限级栏目的排序功能实现方法示例
2020/03/29 PHP
jQuery ui 1.7更新小结
2009/08/15 Javascript
silverlight线程与基于事件驱动javascript引擎(实现轨迹回放功能)
2011/08/09 Javascript
判断多个input type=file是否有已经选择好文件的代码
2012/05/23 Javascript
jQuery实现点击标题输入详细信息
2013/04/16 Javascript
JavaScript弹出窗口方法汇总
2014/08/12 Javascript
JS将滑动门改为选项卡(需鼠标点击)的实现方法
2015/09/27 Javascript
JavaScript深度复制(deep clone)的实现方法
2016/02/19 Javascript
PHP抓取HTTPS内容和错误处理的方法
2016/09/30 Javascript
利用types增强vscode中js代码提示功能详解
2017/07/07 Javascript
微信小程序 上传头像的实例详解
2017/10/27 Javascript
微信小程序框架的页面布局代码
2019/08/17 Javascript
js的新生代垃圾回收知识点总结
2019/08/22 Javascript
Vue+element-ui添加自定义右键菜单的方法示例
2020/12/08 Vue.js
nestjs中异常过滤器Exceptionfilter的具体使用
2021/02/07 Javascript
python实现系统状态监测和故障转移实例方法
2013/11/18 Python
python新手经常遇到的17个错误分析
2014/07/30 Python
Python实现的最近最少使用算法
2015/07/10 Python
解决使用pycharm提交代码时冲突之后文件丢失找回的方法
2018/08/05 Python
python 搭建简单的http server,可直接post文件的实例
2019/01/03 Python
Pytorch实现GoogLeNet的方法
2019/08/18 Python
python数据库编程 Mysql实现通讯录
2020/03/27 Python
Python astype(np.float)函数使用方法解析
2020/06/08 Python
如何用Django处理gzip数据流
2021/01/29 Python
css实例教程 一款纯css3实现的超炫动画背画特效
2014/11/05 HTML / CSS
HTML5 CSS3新的WEB标准和浏览器支持
2009/07/16 HTML / CSS
在求职信中如何凸显个人优势
2013/10/30 职场文书
电气工程及自动化专业自荐书范文
2013/12/18 职场文书
校园安全检查制度
2014/02/03 职场文书
创业计划实施的7大步骤
2014/02/05 职场文书
2015年大学生村官工作总结
2015/04/21 职场文书
听证通知书
2015/04/24 职场文书
同事去世追悼词
2015/06/23 职场文书