python代碼 構建驗證碼

來源:互聯網
上載者:User

標籤:style   and   text   letters   int   編寫   height   div   處理   

1.python代碼編寫 (隨機驗證碼):

 1 #coding: utf-8 2  3 import Image, ImageDraw, ImageFont, ImageFilter 4 import string, random 5  6 fontPath = "/home/itcast/ace/media/" 7  8 # 獲得隨機四個字母 9 def getRandomChar():10     return [random.choice(string.letters) for _ in range(4)]11 12 # 獲得顏色13 def getRandomColor():14     return (random.randint(30, 100), random.randint(30, 100), random.randint(30, 100))15 16 # 獲得驗證碼圖片17 def getCodePiture():18     width = 24019     height = 6020 21     # 建立畫布22     image = Image.new(‘RGB‘, (width, height), (180,180,180))23     font = ImageFont.truetype(fontPath + ‘simhei.ttf‘, 80)24     draw = ImageDraw.Draw(image)25 26     # 建立驗證碼對象27     code = getRandomChar()#code-> [x,A,y,U] 28 29     # 把驗證碼放到畫布上30     for t in range(4):31         draw.text((60 * t + 10, 0), code[t], font=font, fill=getRandomColor())32 33     # 填充噪點34     for _ in range(random.randint(1500,3000)):35         draw.point((random.randint(0,width), random.randint(0,height)), fill=getRandomColor())36 37     # 模糊處理38 #image = image.filter(ImageFilter.BLUR)39 40     # 儲存名字為驗證碼的圖片41     #code = [x,y, U,a] --> xyUa.jpg42     image.save("".join(code) + ‘.jpg‘, ‘jpeg‘);43 44 45 if __name__ == ‘__main__‘:46     getCodePiture()

 

python代碼 構建驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.