python基于opencv批量生成验证码的示例
基本思路是使用opencv来把随机生成的字符,和随机生成的线段,放到一个随机生成的图像中去。
虽然没有加复杂的形态学处理,但是目前看起来效果还不错
尝试生成1000张图片,但是最后只有998张,因为有有重复的,被覆盖掉了。
代码如下:
import cv2import numpy as npline_num = 10pic_num = 1000path = "./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 = 60img_width = 240for 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 批量生成验证码的资料请关注 其它相关文章!
最后更新于 2021-11-23 09:11:10 并被添加「」标签,已有 位童鞋阅读过。
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
相关文章
- 都市牧场-无糖薄荷糖清新口气网红口香糖接吻糖维C水果糖联名礼盒
- 空气炸锅专用纸方形吸油纸碗垫硅油纸盘烧烤箱烘焙家用食品级锡纸
- 如何启用手机微信QQ邮件提醒功能
- 微信读书App如何将书架书设置为私人阅读?
- 晨光蝴蝶夹票夹小号金属蝴蝶夹文件夹铁夹子票据整理夹固定特大号强力万用画板钢夹多功能办公用品小夹子文具