python 生成圖形驗(yàn)證碼的方法示例
日常在網(wǎng)站使用過程中經(jīng)常遇到圖形驗(yàn)證,今天準(zhǔn)備自己做個(gè)圖形驗(yàn)證碼,這算是個(gè)簡(jiǎn)單的功能,也適合新手練習(xí)的,便于自己學(xué)習(xí)。
主要用到的庫--PIL圖像處理庫,簡(jiǎn)單的思路,我們需要隨機(jī)的顏色,隨機(jī)的數(shù)字或字母,隨機(jī)的線條、點(diǎn)作為干擾元素 拼湊成一張圖片。
生成隨機(jī)顏色,返回的是rgb三色。
def getRandomColor(): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) return (r, g, b)
從數(shù)字、大小寫字母里生成隨機(jī)字符。
def getRandomChar(): random_num = str(random.randint(0, 9)) random_lower = chr(random.randint(97, 122)) # 小寫字母a~z random_upper = chr(random.randint(65, 90)) # 大寫字母A~Z random_char = random.choice([random_num, random_lower, random_upper]) return random_char
圖片操作,生成一張隨機(jī)背景色的圖片,隨機(jī)生成5種字符+5種顏色,在圖片上描繪字,由于默認(rèn)的字體很小,還需要對(duì)字進(jìn)行處理,不同系統(tǒng)下的字體文件存放位置不一樣,這里我是把window下的 arial.ttf 字體復(fù)制到了當(dāng)前文件夾下直接使用的。
# 圖片寬高
width = 160
height = 50
def createImg():
bg_color = getRandomColor()
# 創(chuàng)建一張隨機(jī)背景色的圖片
img = Image.new(mode="RGB", size=(width, height), color=bg_color)
# 獲取圖片畫筆,用于描繪字
draw = ImageDraw.Draw(img)
# 修改字體
font = ImageFont.truetype(font="arial.ttf", size=36)
for i in range(5):
# 隨機(jī)生成5種字符+5種顏色
random_txt = getRandomChar()
txt_color = getRandomColor()
# 避免文字顏色和背景色一致重合
while txt_color == bg_color:
txt_color = getRandomColor()
# 根據(jù)坐標(biāo)填充文字
draw.text((10 + 30 * i, 3), text=random_txt, fill=txt_color, font=font)
# 打開圖片操作,并保存在當(dāng)前文件夾下
with open("test.png", "wb") as f:
img.save(f, format="png")
這個(gè)時(shí)候可以看到文件夾下面的圖片

這里是張很清晰的圖片,為了有干擾元素,這里還需要在圖片加入些線條、點(diǎn)作為干擾點(diǎn)。
隨機(jī)畫線,在圖片寬高范圍內(nèi)隨機(jī)生成2個(gè)坐標(biāo)點(diǎn),并通過隨機(jī)顏色產(chǎn)生線條。
def drawLine(draw):
for i in range(5):
x1 = random.randint(0, width)
x2 = random.randint(0, width)
y1 = random.randint(0, height)
y2 = random.randint(0, height)
draw.line((x1, y1, x2, y2), fill=getRandomColor())
隨機(jī)畫點(diǎn),隨機(jī)生成橫縱坐標(biāo)點(diǎn)。
def drawPoint(draw):
for i in range(50):
x = random.randint(0, width)
y = random.randint(0, height)
draw.point((x,y), fill=getRandomColor())
生成方法
def createImg():
bg_color = getRandomColor()
# 創(chuàng)建一張隨機(jī)背景色的圖片
img = Image.new(mode="RGB", size=(width, height), color=bg_color)
# 獲取圖片畫筆,用于描繪字
draw = ImageDraw.Draw(img)
# 修改字體
font = ImageFont.truetype(font="arial.ttf", size=36)
for i in range(5):
# 隨機(jī)生成5種字符+5種顏色
random_txt = getRandomChar()
txt_color = getRandomColor()
# 避免文字顏色和背景色一致重合
while txt_color == bg_color:
txt_color = getRandomColor()
# 根據(jù)坐標(biāo)填充文字
draw.text((10 + 30 * i, 3), text=random_txt, fill=txt_color, font=font)
# 畫干擾線點(diǎn)
drawLine(draw)
drawPoint(draw)
# 打開圖片操作,并保存在當(dāng)前文件夾下
with open("test.png", "wb") as f:
img.save(f, format="png")
最終生成的圖片

這里介紹的是圖片生成的方法,可以將圖片直接顯示在前端,也可以使用接口返回url。用Django做的,需要注意的是圖片保存的路徑。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用pyecharts在jupyter notebook上繪圖
這篇文章主要介紹了使用pyecharts在jupyter notebook上繪圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-07-07
Python中torch.load()加載模型以及其map_location參數(shù)詳解
torch.load()作用用來加載torch.save()保存的模型文件,下面這篇文章主要給大家介紹了關(guān)于Python中torch.load()加載模型以及其map_location參數(shù)的相關(guān)資料,需要的朋友可以參考下2022-09-09
python3 自動(dòng)識(shí)別usb連接狀態(tài),即對(duì)usb重連的判斷方法
今天小編就為大家分享一篇python3 自動(dòng)識(shí)別usb連接狀態(tài),即對(duì)usb重連的判斷方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python中使用select模塊實(shí)現(xiàn)非阻塞的IO
這篇文章主要介紹了Python中使用select模塊實(shí)現(xiàn)非阻塞的IO,本文使用一個(gè)簡(jiǎn)單聊天室程序講解Python中的select模塊使用,需要的朋友可以參考下2015-02-02
Python深度學(xué)習(xí)pyTorch權(quán)重衰減與L2范數(shù)正則化解析
這篇文章主要介紹了Python深度學(xué)習(xí)中的pyTorch權(quán)重衰減與L2范數(shù)正則化的詳細(xì)解析,文中附含詳細(xì)示例代碼,有需要的朋友可以借鑒參考下2021-09-09
用Python+OpenCV對(duì)比圖像質(zhì)量的幾種方法
這篇文章主要介紹了用Python+OpenCV對(duì)比圖像質(zhì)量過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07

