python opencv 生成验证码
基本思路是使用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随机生成验证码的两种方法
Python随机生成验证码的方法有很多,今天给大家列举两种,大家也可以在这个基础上进行改造,设计出适合自己的验证码方法方法一:利用range Python随机生成验证码的方法有很多,今天给大家列举两种 ...
- python快速生成验证码
利用python库random,string生成大小写字母和数字的随机验证码 import random import string def generate_code(bit_num): ''' : ...
- python生成验证码脚本
最近每天都用python写一个小的脚本,练习使用python语法. 验证码的生成: 这里使用了python的图像处理库PIL,安装PIL的过程中出了一个小麻烦,就使用Pillow-win32的一个文件 ...
- Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed
最近在用Python开发自己的博客,需要用到Python生成验证码,当然肯定要用到Python的图形处理库PIL,因为我用的是windows. 所以在安装好pil之后就开始写,就按照题目所说出现了Th ...
- python的random模块(生成验证码)
python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) # ...
- Python 使用Pillow模块生成验证码
1.安装 pip3 install pillow 2.使用步骤 生成验证码和验证字符串 绘制图片,将验证码放入session中 将图片返回给页面 3.代码demo #!/usr/bin/env pyt ...
- 随机生成验证码及python中的事务
1.随机生成验证码 # import random # print(random.random()) #-1的小数 # print(random.randint(,)) #包括1和3 # print( ...
- Python 代码实现验证码识别
Python 代码实现验证码识别 测试开发社区 1周前 源 / j_hao104 一.探讨 识别图形验证码可以说是做爬虫的必修课,涉及到计算机图形学,机器学习,机器视觉,人工智能等等高深领域…… ...
- python快速生成注释文档的方法
python快速生成注释文档的方法 今天将告诉大家一个简单平时只要注意的小细节,就可以轻松生成注释文档,也可以检查我们写的类方法引用名称是否重复有问题等.一看别人专业的大牛们写的文档多牛多羡慕,不用担 ...
随机推荐
- linux的vsftp使用介绍
vsftp 常用操作 安装 yum install vsftpd 启动服务 service vsftpd start 开启自动启动 chkconfig --level 35 vsftpd on 关闭匿 ...
- Codeforces Round #392 (Div. 2) - B
题目链接:http://codeforces.com/contest/758/problem/B 题意:给定n个点灯的情况,灯只有四种颜色RBGY,然后如果某个灯坏了则用'!'表示,现在要求将坏的灯( ...
- [转载]解决Android studio新建项目慢的问题
原文地址为:https://blog.csdn.net/easion_zms/article/details/73181402 Android Studio 好处很多,但是当从github上或者导入其 ...
- Spring MVC 跳转失败,但配置正确填坑
1:正确写法 @RequestMapping("{type_key}.html") public String geren(Model model, @PathVariable S ...
- bzoj2560 串珠子 状压DP
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2560 题解 大概是这类关于无向图的联通性计数的套路了. 一开始我想的是这样的,考虑容斥,那么就 ...
- 日常Git使用——2019年12月11日16:19:03
1.git介绍 1.1 什么是git? 什么是Git? 比如一个项目,两个人同时参与开发,那么就把这个项目放在一个公共的地方,需要的时候都可以去获取,有什么改动,都可以进行提交. 为了做到这一点,就需 ...
- CentOS7.5 安装部署Apache+Mysql+Php
系统:CentOS7.5 安装Apache 安装 yum -y install httpd 开启apache服务 systemctl start httpd.service 设置apache服务开机启 ...
- SpringBoot JSON文件读取
@Componentpublic class StepExecutor implements Runnable { @Value("classpath:menu.json") pr ...
- TypeError:NoneType object is not callable情况下的错误
只用在该视图函数下面加上return ‘值’
- visual studio code的使用
1.添加代码片段 参考:https://blog.csdn.net/qq_36370731/article/details/83014439 2.在vscode上运行Git 先打开vscode内置的G ...