python基于opencv批量生成驗證碼的示例
更新時間:2021年04月28日 11:43:04 作者:奈何緣淺wyj
這篇文章主要介紹了python基于opencv批量生成驗證碼的示例,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下
基本思路是使用opencv來把隨機生成的字符,和隨機生成的線段,放到一個隨機生成的圖像中去。
雖然沒有加復雜的形態(tài)學處理,但是目前看起來效果還不錯
嘗試生成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批量生成驗證碼的示例的詳細內(nèi)容,更多關于python 批量生成驗證碼的資料請關注腳本之家其它相關文章!
相關文章
Python批量實現(xiàn)word中查找關鍵字的示例代碼
本文主要介紹了Python批量實現(xiàn)word中查找關鍵字的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07
Python實現(xiàn)常見數(shù)據(jù)格式轉換的方法詳解
這篇文章主要為大家詳細介紹了Python實現(xiàn)常見數(shù)據(jù)格式轉換的方法,主要是xml_to_csv和csv_to_tfrecord,感興趣的小伙伴可以了解一下2022-09-09

