python3 pillow模块实现简单验证码

yipeiwu_com6年前Python基础

本文实例为大家分享了python3 pillow模块验证码的具体代码,供大家参考,具体内容如下

直接放代码吧,该写的注释基本都写了

# -*- coding: utf-8 -*-
# __author__: Pad0y

from PIL import Image, ImageDraw, ImageFont
from random import choice, randint, randrange
import string

# 候选字符集,大小写字母+数字
chrs = string.ascii_letters + string.digits


def selected_chrs(length):
  """
  返回length个随机字符串
  :param length:
  :return:
  """
  result = ''.join(choice(chrs) for _ in range(length))
  return result


def get_color():
  """
  设置随机颜色
  :return:
  """
  r = randint(0, 255)
  g = randint(0, 255)
  b = randint(0, 255)
  return (r, g, b)


def main(size=(200, 100), chrNumber=6, bgcolor=(255, 255, 255)):
  """
  定义图片大小,验证码长度,背景颜色
  :param size:
  :param chrNumber:
  :param bgcolor:
  :return:
  """
  # 创建空白图像和绘图对象
  image_tmp = Image.new('RGB', size, bgcolor)
  draw = ImageDraw.Draw(image_tmp)

  # 生成并计算随机字符的宽度和高度
  text = selected_chrs(chrNumber)
  font = ImageFont.truetype('c:\\windows\\fonts\\Roboto-Regular.ttf', 48) # 选定一款系统字体
  width, height = draw.textsize(text, font)
  if width + 2*chrNumber > size[0] or height > size[1]:
    print('Size Error!')
    return

  # 绘制字符串
  startX = 0
  width_eachchr = width // chrNumber # 计算每个字符宽度
  for i in range(chrNumber):
    startX += width_eachchr + 1
    position = (startX, (size[1]-height)//2+randint(-10, 10)) # 字符坐标, Y坐标上下浮动
    draw.text(xy=position, text=text[i], font=font, fill=get_color()) # 绘制函数

  # 对像素位置进行微调,实现验证码扭曲效果
  img_final = Image.new('RGB', size, bgcolor)
  pixels_final = img_final.load()
  pixels_tmp = image_tmp.load()
  for y in range(size[1]):
    offset = randint(-1, 0) # randint()相当于闭区间[x,y]
    for x in range(size[0]):
      newx = x + offset # 像素微调
      if newx >= size[0]:
        newx = size[0] - 1
      elif newx < 0:
        newx = 0
      pixels_final[newx, y] = pixels_tmp[x, y]

  # 绘制随机颜色随机位置的干扰像素
  draw = ImageDraw.Draw(img_final)
  for i in range(int(size[0]*size[1]*0.07)): # 7%密度的干扰像素
    draw.point((randrange(size[0]), randrange(size[1])), fill=get_color()) # randrange取值范围是左开右闭

  # 绘制随机干扰线,这里设置为8条
  for i in range(8):
    start = (0, randrange(size[1]))
    end = (size[0], randrange(size[1]))
    draw.line([start, end], fill=get_color(), width=1)

  # 绘制随机弧线
  for i in range(8):
    start = (-50, -50) # 起始位置在外边看起来才会像弧线
    end = (size[0]+10, randint(0, size[1]+10))
    draw.arc(start+end, 0, 360, fill=get_color())

  # 保存图片
  img_final.save('Veri_code.jpg')
  img_final.show()


if __name__ == '__main__':
  main((200, 100), 6, (255, 255, 255))

效果图如下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

在matplotlib的图中设置中文标签的方法

在matplotlib的图中设置中文标签的方法

其实就是通过 FontProperties来设置的,请参考以下代码: import matplotlib.pyplot as plt from matplotlib.font_man...

Python快速转换numpy数组中Nan和Inf的方法实例说明

在使用numpy数组的过程中时常会出现nan或者inf的元素,可能会造成数值计算时的一些错误。这里提供一个numpy库函数的用法,使nan和inf能够最简单地转换成相应的数值。 num...

Python实现二分法算法实例

1.算法:(设查找的数组期间为array[low, high]) (1)确定该期间的中间位置K (2)将查找的值T与array[k]比较。若相等,查找成功返回此位置;否则确定新的查找区域...

python difflib模块示例讲解

python difflib模块示例讲解

difflib模块提供的类和方法用来进行序列的差异化比较,它能够比对文件并生成差异结果文本或者html格式的差异化比较页面,如果需要比较目录的不同,可以使用filecmp模块。 clas...

django使用LDAP验证的方法示例

django使用LDAP验证的方法示例

1.安装Python-LDAP(python_ldap-2.4.25-cp27-none-win_amd64.whl)pip install python_ldap-2.4.25-cp2...