pillow模块(图像处理,生成验证码)

一、pillow的简单使用

  1.1 安装:pip3 install pillow

  1.2 图片的创建:

from PIL import Image
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))  # size:长=120px,宽=30px
 
# 在图片查看器中打开
# img.show() 
 
# 保存在本地
with open('code.png','wb') as f:
    img.save(f,format='png')

  1.3 创建画笔对象:

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))  # 图片对象
draw = ImageDraw.Draw(img, mode='RGB')  # 通过图片对象生成一个画笔对象

  1.4 画点:

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示坐标
# 第二个参数:表示颜色
draw.point([100, 100], fill="red")  # 坐标是画笔对象从左上角开始,x=100px,y=100px, fill是画布颜色
draw.point([300, 300], fill=(255, 255, 255))

  1.5 画线:

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示起始坐标和结束坐标
# 第二个参数:表示颜色
draw.line((100,100,100,300), fill='red')   # line方法画线
draw.line((100,100,300,100), fill=(255, 255, 255))

  1.6 画圆&画弧线:

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示起始坐标和结束坐标(圆要画在其中间)
# 第二个参数:表示开始角度
# 第三个参数:表示结束角度
# 第四个参数:表示颜色
draw.arc((100,100,300,300),0,90,fill="red")

  1.7 画布写字体:

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
draw.text([0,0],'python',"red")  # 字体内容:python

  1.8 特殊字体,指定字体格式:

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一个参数:表示字体文件路径
# 第二个参数:表示字体大小
font = ImageFont.truetype("kumo.ttf", 28)  # 可以是字体包的路径,一般是.ttf格式的字体文件,
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
# 第四个参数:表示颜色
draw.text([0, 0], 'python', "red", font=font)

  1.9 文件操作:

# 通过对片对象打开图片文件img.show()

# 写入图片文件with open('code.png','wb') as f:    img.save(f,format='png')


# 3. 写入内存(Python3)
from io import BytesIO  # 导入方式
stream = BytesIO()
img.save(stream, 'png')
stream.getvalue()


# 4. 写入内存(Python2)
import StringIO
stream = StringIO.StringIO()
img.save(stream, 'png')
stream.getvalue()

二、pillow实例(生成验证码)

# 验证码
def get_valid_img(request):
    # 生成三个随机数0-110之间
    def get_random_color():
        return (random.randint(0, 110), random.randint(0, 110), random.randint(0, 110))

    def get_random_back_color():
        return (random.randint(110, 255), random.randint(110, 255), random.randint(110, 255))

    # 图片对象
    img_obj = Image.new('RGB', (140, 34), get_random_back_color())  # 此处使用register1.html文件
    draw_obj = ImageDraw.Draw(img_obj)  # 通过图片对象生成一个画笔对象
    # font_path = os.path.join(settings.BASE_DIR, "statics/font/cerepf__.ttf")  # 获取字体,注意有些字体文件不能识别数字,所以如果数字显示不出来,就换个字体
    font_obj = ImageFont.truetype("statics/font/cerepf__.ttf", 28)  # 创建字体对象
    sum_str = ''  # 这个数据就是用户需要输入的验证码的内容
    for i in range(1, 5):
        a = random.choice(
            [str(random.randint(0, 9)), chr(random.randint(97, 122)), chr(random.randint(65, 90))])  # 4  a  5  D
        # draw_obj.text((i * 35, 0), a, fill=get_random_color(), font=font_obj)   # 此处使用register.html
        draw_obj.text((i * 22, 0), a, fill=get_random_color(), font=font_obj)  # 此处使用register1.html
        sum_str += a
    print(sum_str)
    # draw_obj.text((64, 10), sum_str, fill=get_random_color(), font=font_obj)  # 通过画笔对象,添加文字

    width = 200
    height = 34
    # 添加噪线
    for i in range(5):  # 添加了5条线
        # 一个坐标表示一个点,两个点就可以连成一条线
        x1 = random.randint(0, width)
        x2 = random.randint(0, width)
        y1 = random.randint(0, height)
        y2 = random.randint(0, height)
        draw_obj.line((x1, y1, x2, y2), fill=get_random_color())
    # 添加噪点
    for i in range(10):
        # 这是添加点,50个点
        draw_obj.point([random.randint(0, width), random.randint(0, height)], fill=get_random_color())
        # 下面是添加很小的弧线,看上去类似于一个点,50个小弧线
        x = random.randint(0, width)
        y = random.randint(0, height)
        draw_obj.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_color())  # x, y是弧线的起始点位置,x + 4, y + 4是弧线的结束点位置

    from io import BytesIO
    f = BytesIO()  # 操作内存的句柄
    img_obj.save(f, 'png')
    data = f.getvalue()

    # 存这个验证码的方式1:赋值给全局变量的简单测试
    # global valid_str
    # valid_str = sum_str
    # 方式2:将验证码存在各用户自己的session中,session的应用其实还有很多
    request.session['valid_str'] = sum_str
    return HttpResponse(data)

 

posted @ 2019-06-24 16:29  Amorphous  阅读(486)  评论(0编辑  收藏  举报