生成隨機圖片驗證碼

#!/usr/bin/env python3 python

#-*- coding:utf-8 -*- dom

 

#經常使用第三方模塊spa

#PIL: Python Imaging Library,已是Python平臺事實上的圖像處理標準庫了。PIL功能很是強大,但API卻很是簡單易用。code

#因爲PIL僅支持到Python2.7,加上年久失修,因而一羣志願者在PIL的基礎上建立了兼容版本Pillow,支持最新的Python 3.X ,又加入了許多新特性,所以,對象

#咱們能夠直接安裝使用Pillow.生成隨機驗證碼:圖片

from PIL import Image,ImageDraw,ImageFont,ImageFilterutf-8

import random 圖像處理

#隨機字母驗證碼

def rndChar():import

    return chr(random.randint(65,90))

 #隨機顏色1:

def rndColor():

    return(random.randint(64,255),random.randint(64,255),random.randint(64,255))

#隨機顏色2

def rndColor2():

    return(random.randint(32,127),random.randint(32,127),random.randint(32,127))

 

#240*60:

width=60*4

height=60

image=Image.new('RGB',(width,height),(255,255,255))

 

#建立pont對象:

font=ImageFont.truetype('e:/work/chinese.TTF',36)

#建立Drev對象:

draw=ImageDraw.Draw(image)

#填充每一個像素:

for x in range(width):

    for y in range(height):

     draw.point((x,y),fill=rndColor())

#輸出文字:

for t in range(4):

    draw.text((60*t+10,10),rndChar(),font=font,fill=rndColor2())

#模糊:

image=image.filter(ImageFilter.BLUR)

image.save('code.jpg','jpeg')

#咱們用隨機顏色填充背景,再畫上文字,最後對圖像進行模糊,獲得驗證碼圖片。

相關文章
相關標籤/搜索