標籤: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代碼 構建驗證碼