欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3 pillow模塊實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼

 更新時(shí)間:2019年10月31日 09:45:30   作者:Pad0y  
這篇文章主要為大家詳細(xì)介紹了python3 pillow模塊實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python3 pillow模塊驗(yàn)證碼的具體代碼,供大家參考,具體內(nèi)容如下

直接放代碼吧,該寫的注釋基本都寫了

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

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

# 候選字符集,大小寫字母+數(shù)字
chrs = string.ascii_letters + string.digits


def selected_chrs(length):
  """
  返回length個(gè)隨機(jī)字符串
  :param length:
  :return:
  """
  result = ''.join(choice(chrs) for _ in range(length))
  return result


def get_color():
  """
  設(shè)置隨機(jī)顏色
  :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)):
  """
  定義圖片大小,驗(yàn)證碼長(zhǎng)度,背景顏色
  :param size:
  :param chrNumber:
  :param bgcolor:
  :return:
  """
  # 創(chuàng)建空白圖像和繪圖對(duì)象
  image_tmp = Image.new('RGB', size, bgcolor)
  draw = ImageDraw.Draw(image_tmp)

  # 生成并計(jì)算隨機(jī)字符的寬度和高度
  text = selected_chrs(chrNumber)
  font = ImageFont.truetype('c:\\windows\\fonts\\Roboto-Regular.ttf', 48) # 選定一款系統(tǒng)字體
  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 # 計(jì)算每個(gè)字符寬度
  for i in range(chrNumber):
    startX += width_eachchr + 1
    position = (startX, (size[1]-height)//2+randint(-10, 10)) # 字符坐標(biāo), Y坐標(biāo)上下浮動(dòng)
    draw.text(xy=position, text=text[i], font=font, fill=get_color()) # 繪制函數(shù)

  # 對(duì)像素位置進(jìn)行微調(diào),實(shí)現(xiàn)驗(yàn)證碼扭曲效果
  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()相當(dāng)于閉區(qū)間[x,y]
    for x in range(size[0]):
      newx = x + offset # 像素微調(diào)
      if newx >= size[0]:
        newx = size[0] - 1
      elif newx < 0:
        newx = 0
      pixels_final[newx, y] = pixels_tmp[x, y]

  # 繪制隨機(jī)顏色隨機(jī)位置的干擾像素
  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取值范圍是左開右閉

  # 繪制隨機(jī)干擾線,這里設(shè)置為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)

  # 繪制隨機(jī)弧線
  for i in range(8):
    start = (-50, -50) # 起始位置在外邊看起來才會(huì)像弧線
    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))

效果圖如下

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論