學習python:執行個體2.用PIL產生隨機驗證碼

來源:互聯網
上載者:User

標籤:驗證碼   python   pil   

效果:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/8D/09/wKiom1iDBSjQ3eUJAAAKu2aEfLk561.gif" title="yanzhengma.gif" alt="wKiom1iDBSjQ3eUJAAAKu2aEfLk561.gif" />


代碼:

# 產生隨機驗證碼圖片import stringfrom random import randint, samplefrom PIL import Image, ImageDraw, ImageFont, ImageFilter# Image 負責處理圖片# ImageDraw 畫筆# ImageFont 文字# ImageFileter 濾鏡# 定義變數img_size = (150,50)        # 定義畫布大小img_rgb = (255,255,255)    # 定義畫布顏色,白色img = Image.new("RGB",img_size,img_rgb)img_text = " ".join(sample(string.ascii_letters+string.digits, 5))# print(img_text.replace(‘ ‘,‘‘))# 畫圖drow = ImageDraw.Draw(img)for i in range(10):    # 隨機畫線    drow.line([tuple(sample(range(img_size[0]),2)), tuple(sample(range(img_size[0]),2))], fill=(0,0,0))for i in range(99):    # 隨機畫點    drow.point(tuple(sample(range(img_size[0]),2)), fill=(0,0,0))# 文字font = ImageFont.truetype("simsun.ttc", 24)     # 定義文字字型和大小drow.text((6,6), img_text, font=font, fill="green")# 扭曲圖片和濾鏡params = [    1 - float(randint(1,2)) / 100,    0,    0,    0,    1 - float(randint(1,10)) /100,    float(randint(1,2)) / 500,    0.001,    float(randint(1,2)) / 500]img = img.transform(img_size, Image.PERSPECTIVE, params)img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)# 展示圖片img.show()


本文出自 “毛線的linux之路” 部落格,請務必保留此出處http://maoxian.blog.51cto.com/4227070/1893551

學習python:執行個體2.用PIL產生隨機驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.