python基于opencv批量生成验证码的示例
更新时间:2021年04月28日 11:43:04 作者:奈何缘浅wyj
这篇文章主要介绍了python基于opencv批量生成验证码的示例,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
基本思路是使用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处理INI、YAML和JSON配置文件
在软件开发中,配置文件是存储程序配置信息的常见方式,INI、YAML和JSON是常用的配置文件格式,各自有着特定的结构和用途,Python拥有丰富的库和模块,本文将重点探讨如何使用Python处理这三种格式的配置文件,需要的朋友可以参考下2023-12-12使用sklearn之LabelEncoder将Label标准化的方法
今天小编就为大家分享一篇使用sklearn之LabelEncoder将Label标准化的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-07-07一文带你深入理解Python的`functools.lru_cache`装饰器
Python中的functools.lru_cache装饰器是一个非常有用的装饰器,它可以帮助我们优化递归函数,避免重复计算已经计算过的值,在这篇文章中,我们将探讨 functools.lru_cache 的工作原理以及如何使用它,感兴趣的朋友跟着小编一起来学习吧2023-07-07django+celery+RabbitMQ自定义多个消息队列的实现
本文主要介绍了django+celery+RabbitMQ自定义多个消息队列的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-02-02
最新评论