Python练习题第 0010题

https://github.com/Yixiaohan/show-me-the-code
第 0010 题:使用 Python 生成类似于下图中的字母验证码图片
这里写图片描述

借鉴了这位仁兄的代码,他的思路很简洁,赞~
https://github.com/Show-Me-the-Code/python/blob/master/NKUCodingCat/0010/0010.py

#! /usr/bin/env python
#! -*- coding: utf-8 -*-

__author__ = 'Sophie2805'

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from pylab import *
import random,numpy,string

path = "/Users/Sophie/PycharmProjects/Practice_0010"
# generate 4 ramdom letters
text = random.sample(string.letters,4)
#print text

# generate 3-D random array
rawArray = numpy.zeros((100,300,3),dtype=numpy.uint8)
sh = rawArray.shape
for i in range(sh[0]):
    for j in range(sh[1]):
        for k in range(sh[2]):
            rawArray[i][j][k]=random.randint(0,255)

# generate the background pic from 3-D array
im = Image.fromarray(rawArray)
draw = ImageDraw.Draw(im)

# add check code onto the background
for i in range(len(text)):
    draw.text((75*i+random.randint(0,40),random.randint(0,40)), text[i],
              font=ImageFont.truetype("Apple Symbols.ttc",60),
              fill = (random.randint(0,255),random.randint(0,255),random.randint(0,255)))

im.save(path+"/checkcode.jpg")

我的代码生成的验证码图片如下所示:
这里写图片描述

Python数组

http://blog.csdn.net/sunny2038/article/details/9002531

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐