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 相关文章推荐
pymongo实现控制mongodb中数字字段做加法的方法
Mar 26 Python
python3使用urllib模块制作网络爬虫
Apr 08 Python
利用python微信库itchat实现微信自动回复功能
May 18 Python
简单实现python聊天程序
Apr 01 Python
详解django中使用定时任务的方法
Sep 27 Python
对Python实现累加函数的方法详解
Jan 23 Python
Python多项式回归的实现方法
Mar 11 Python
python and or用法详解
Jun 26 Python
python中类的输出或类的实例输出为这种形式的原因
Aug 12 Python
PHP统计代码行数的小代码
Sep 19 Python
Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件封装函数的方法
Sep 23 Python
Python能做什么
Jun 02 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正则替换处理HTML页面的方法
2015/06/17 PHP
Zend Framework教程之Application用法实例详解
2016/03/14 PHP
laravel实现查询最后执行的一条sql语句的方法
2019/10/09 PHP
JavaScript入门教程(12) js对象化编程
2009/01/31 Javascript
Javascript Jquery 遍历Json的实现代码
2010/03/31 Javascript
jQuery表格行换色的三种实现方法
2011/06/27 Javascript
ASP.NET jQuery 实例15 通过控件CustomValidator验证CheckBoxList
2012/02/03 Javascript
jQuery fadeTo方法调整图片的透明度使用介绍
2013/05/06 Javascript
详解JavaScript的while循环的使用
2015/06/03 Javascript
Javascript对象Clone实例分析
2015/06/09 Javascript
网页收藏夹显示ICO图标(代码少)
2015/08/04 Javascript
JS实现网页顶部向下滑出的全国城市切换导航效果
2015/08/22 Javascript
javascript实现uploadify上传格式以及个数限制
2015/11/23 Javascript
JS实现控制图片显示大小的方法【图片等比例缩放功能】
2017/02/18 Javascript
教你如何编写Vue.js的单元测试的方法
2018/10/17 Javascript
利用Angular2的Observables实现交互控制的方法
2018/12/27 Javascript
解决vue项目F5刷新mounted里的函数不执行问题
2019/11/05 Javascript
在Python的Flask框架中实现全文搜索功能
2015/04/20 Python
Python科学计算之NumPy入门教程
2017/01/15 Python
Python for循环生成列表的实例
2018/06/15 Python
python使用thrift教程的方法示例
2019/03/21 Python
详解python持久化文件读写
2019/04/06 Python
Python实现 PS 图像调整中的亮度调整
2019/06/28 Python
python+selenium+chrome批量文件下载并自动创建文件夹实例
2020/04/27 Python
美国打印机墨水和碳粉购物网站:QuikShip Toner
2018/08/29 全球购物
如何实现jdbc性能优化
2012/07/30 面试题
校园门卫岗位职责
2013/12/09 职场文书
组织生活会发言材料
2014/12/15 职场文书
教育实习指导教师评语
2014/12/31 职场文书
语文教师求职信范文
2015/03/20 职场文书
女性健康知识讲座通知
2015/04/23 职场文书
公司员工违纪检讨书
2015/05/05 职场文书
2016年大学生实习单位评语
2015/12/01 职场文书
2016简单的租房合同范本
2016/03/18 职场文书
在Python中如何使用yield
2021/06/07 Python
JavaScript的Set数据结构详解
2022/02/18 Javascript