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中的进程分支fork和exec详解
Apr 11 Python
浅谈五大Python Web框架
Mar 20 Python
详解python eval函数的妙用
Nov 16 Python
Python numpy生成矩阵、串联矩阵代码分享
Dec 04 Python
python正则表达式及使用正则表达式的例子
Jan 22 Python
Python绘制KS曲线的实现方法
Aug 13 Python
解决Django Static内容不能加载显示的问题
Jul 28 Python
python实现名片管理器的示例代码
Dec 17 Python
Python常用模块函数代码汇总解析
Aug 31 Python
Python的Tqdm模块实现进度条配置
Feb 24 Python
Python中常见的反爬机制及其破解方法总结
Jun 10 Python
浅谈哪个Python库才最适合做数据可视化
Jun 28 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
火车采集器 免费版使出收费版本功能实现原理
2009/09/17 PHP
php 5.6版本中编写一个PHP扩展的简单示例
2015/01/20 PHP
thinkPHP查询方式小结
2016/01/09 PHP
EasyUI的treegrid组件动态加载数据问题的解决办法
2011/12/11 Javascript
JavaSript中变量的作用域闭包的深入理解
2014/05/12 Javascript
两种方法基于jQuery实现IE浏览器兼容placeholder效果
2014/10/14 Javascript
jquery判断单选按钮radio是否选中的方法
2015/05/05 Javascript
jQuery+HTML5实现手机摇一摇换衣特效
2015/06/05 Javascript
JavaScript操作class和style样式代码详解
2016/02/13 Javascript
Bootstrap carousel轮转图的使用实例详解
2016/05/17 Javascript
怎么引入(调用)一个JS文件
2016/05/26 Javascript
微信小程序 http请求的session管理
2017/06/07 Javascript
原生js封装添加class,删除class的实例
2017/11/06 Javascript
TypeScript基础入门教程之三重斜线指令详解
2018/10/22 Javascript
微信小程序局部刷新触发整页刷新效果的实现代码
2018/11/21 Javascript
vue.js 打包时出现空白页和路径错误问题及解决方法
2019/06/26 Javascript
jQuery实现全选、反选和不选功能的方法详解
2019/12/04 jQuery
vue中父子组件的参数传递和应用示例
2021/01/04 Vue.js
[48:21]Mski vs VGJ.S Supermajor小组赛C组 BO3 第一场 6.3
2018/06/04 DOTA
python批量提交沙箱问题实例
2014/10/08 Python
Python实现的矩阵类实例
2017/08/22 Python
python爬虫爬取淘宝商品信息
2018/02/23 Python
详解PyTorch批训练及优化器比较
2018/04/28 Python
为什么Python中没有"a++"这种写法
2018/11/27 Python
将pip源更换到国内镜像的详细步骤
2019/04/07 Python
提升Python效率之使用循环机制代替递归函数
2019/07/23 Python
如何在Windows中安装多个python解释器
2020/06/16 Python
土木工程毕业生推荐信
2013/10/28 职场文书
关于毕业的广播稿
2014/01/10 职场文书
国贸专业的职业规划范文
2014/01/23 职场文书
临床护理求职信
2014/04/26 职场文书
槐乡的孩子教学反思
2014/04/27 职场文书
2014年廉洁自律承诺书
2014/05/26 职场文书
教师工作决心书
2015/02/04 职场文书
蜗居观后感
2015/06/11 职场文书
2019年妇科护士的自我鉴定(3篇)
2019/09/26 职场文书