python基于opencv批量生成驗證碼的示例
基本思路是使用opencv來把隨機(jī)生成的字符,和隨機(jī)生成的線段,放到一個隨機(jī)生成的圖像中去。
雖然沒有加復(fù)雜的形態(tài)學(xué)處理,但是目前看起來效果還不錯
嘗試生成1000張圖片,但是最后只有998張,因為有有重復(fù)的,被覆蓋掉了。
代碼如下:
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 = "" #生成一個隨機(jī)矩陣,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()
結(jié)果:
以上就是python基于opencv批量生成驗證碼的示例的詳細(xì)內(nèi)容,更多關(guān)于python 批量生成驗證碼的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python批量實現(xiàn)word中查找關(guān)鍵字的示例代碼
本文主要介紹了Python批量實現(xiàn)word中查找關(guān)鍵字的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Python實現(xiàn)常見數(shù)據(jù)格式轉(zhuǎn)換的方法詳解
這篇文章主要為大家詳細(xì)介紹了Python實現(xiàn)常見數(shù)據(jù)格式轉(zhuǎn)換的方法,主要是xml_to_csv和csv_to_tfrecord,感興趣的小伙伴可以了解一下2022-09-09利用Python進(jìn)行音頻信號處理和音樂生成的代碼示例
隨著計算機(jī)技術(shù)的快速發(fā)展,音頻信號處理和音樂生成逐漸成為了Python程序員們的關(guān)注點,本文將介紹如何利用Python進(jìn)行音頻信號處理和音樂生成,包括基本概念、常用庫的使用以及實際的代碼示例,需要的朋友可以參考下2024-06-06