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

Python簡單的制作圖片驗證碼實例

 更新時間:2017年05月31日 10:25:18   作者:Woo93  
本篇文章主要介紹了Python簡單的制作圖片驗證碼實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下

這里示范的驗證碼都是簡單的,你也可以把字符扭曲


Python第三方庫無比強大,PIL 是python的一個d第三方圖片處理模塊,我們也可以使用它來生成圖片驗證碼

PIL安裝

命令安裝:

pip install pillow

下載源碼安裝:

復(fù)制地址 :https://github.com/python-pillow/Pillow

PIL使用

例子:生成圖片,并填充文字

#!/usr/bin/python
#-*-coding:utf-8-*-
from PIL import Image, ImageDraw, ImageFont, ImageFilter
# 實例一個圖片對象240 x 60:
width = 60 * 4
height = 60
# 圖片顏色
clo = (43, 34, 88) # 我覺得是紫藍色
image = Image.new('RGB', (width, height), clo)

# 創(chuàng)建Font對象:
# 字體文件可以使用操作系統(tǒng)的,也可以網(wǎng)上下載
font = ImageFont.truetype('./font/Arial.ttf', 36)

# 創(chuàng)建Draw對象:
draw = ImageDraw.Draw(image)

# 輸出文字:
str1 = "ren ren Python"
w = 4 #距離圖片左邊距離
h = 10 #距離圖片上邊距離
draw.text((w, h), str1, font=font)
# 模糊:
image.filter(ImageFilter.BLUR)
code_name = 'test_code_img.jpg'
save_dir = './{}'.format(code_name)
image.save(save_dir, 'jpeg')
print("已保存圖片: {}".format(save_dir))
(venv) allenwoo@~/renren/code$ python test2.py 
已保存圖片: ./test_code_img.jpg

圖片如下:

文字沒有什么色彩,我們也可以加上顏色,只需要在 text 中傳人 fill 參數(shù)就好

draw.text((w, h), str1, font=font, fill = (78, 64, 65))

隨便加的顏色


我們還可以把背景弄成很多個小點,每隔n隔像素填充個其他顏色比如:

#!/usr/bin/python
#-*-coding:utf-8-*-
from PIL import Image, ImageDraw, ImageFont, ImageFilter
# 實例一個圖片對象240 x 60:
width = 60 * 4
height = 60
# 圖片顏色
clo = (43, 34, 88) # 我覺得是紫藍色
image = Image.new('RGB', (width, height), clo)

# 創(chuàng)建Font對象:
# 字體文件可以使用操作系統(tǒng)的,也可以網(wǎng)上下載
font = ImageFont.truetype('./font/Arial.ttf', 36)

# 創(chuàng)建Draw對象:
draw = ImageDraw.Draw(image)

# 填充像素:
# 寬每隔 20, 高每隔5, 形成坐標(biāo)x,y
# 紅色:220,20,60
for x in range(0, width, 20):
  for y in range(0, height, 5):
    draw.point((x, y), fill=(220, 20, 60))

# 輸出文字:
str1 = "we are renren"
w = 4 #距離圖片左邊距離
h = 10 #距離圖片上邊距離
draw.text((w, h), str1, font=font, fill = (78, 64, 65))
# 模糊:
image.filter(ImageFilter.BLUR)
code_name = 'test_code_img.jpg'
save_dir = './{}'.format(code_name)
image.save(save_dir, 'jpeg')
print("已保存圖片: {}".format(save_dir))

結(jié)果圖片:


PIL制作驗證碼

利用以上這些,還有我們之前學(xué)習(xí)的隨機生成器random就可以做個驗證碼了,

生成驗證碼代碼

#!/usr/bin/python
#-*-coding:utf-8-*-
from uuid import uuid1
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random
def rnd_char():
  '''
  隨機一個字母或者數(shù)字
  :return: 
  '''
  # 隨機一個字母或者數(shù)字
  i = random.randint(1,3)
  if i == 1:
    # 隨機個數(shù)字的十進制ASCII碼
    an = random.randint(97, 122)
  elif i == 2:
    # 隨機個小寫字母的十進制ASCII碼
    an = random.randint(65, 90)
  else:
    # 隨機個大寫字母的十進制ASCII碼
    an = random.randint(48, 57)
  # 根據(jù)Ascii碼轉(zhuǎn)成字符,return回去
  return chr(an)

# 干擾
def rnd_dis():
  '''
  隨機一個干擾字
  :return: 
  '''
  d = ['^','-', '~', '_', '.']
  i = random.randint(0, len(d)-1)
  return d[i]

# 兩個隨機顏色都規(guī)定不同的區(qū)域,防止干擾字符和驗證碼字符顏色一樣
# 隨機顏色1:
def rnd_color():
  '''
  隨機顏色,規(guī)定一定范圍
  :return: 
  '''
  return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))

# 隨機顏色2:
def rnd_color2():
  '''
   隨機顏色,規(guī)定一定范圍
   :return: 
   '''
  return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))

def create_code():
  # 240 x 60:
  width = 60 * 4
  height = 60
  image = Image.new('RGB', (width, height), (192, 192, 192))
  # 創(chuàng)建Font對象:
  font = ImageFont.truetype('./font/Arial.ttf', 36)

  # 創(chuàng)建Draw對象:
  draw = ImageDraw.Draw(image)

  # 填充每個像素:
  for x in range(0, width, 20):
    for y in range(0, height, 10):
      draw.point((x, y), fill=rnd_color())

  # 填充字符
  _str = ""
  # 填入4個隨機的數(shù)字或字母作為驗證碼
  for t in range(4):
    c = rnd_char()
    _str = "{}{}".format(_str, c)

    # 隨機距離圖片上邊高度,但至少距離30像素
    h = random.randint(1, height-30)
    # 寬度的化,每個字符占圖片寬度1/4,在加上10個像素空隙
    w = width/4 * t + 10
    draw.text((w, h), c, font=font, fill=rnd_color2())

  # 實際項目中,會將驗證碼 保存在數(shù)據(jù)庫,并加上時間字段
  print("保存驗證碼 {} 到數(shù)據(jù)庫".format(_str))

  # 給圖片加上字符干擾,密集度由 w, h控制
  for j in range(0, width, 30):
    dis = rnd_dis()
    w = t * 15 + j

    # 隨機距離圖片上邊高度,但至少距離30像素
    h = random.randint(1, height - 30)
    draw.text((w, h), dis, font=font, fill=rndColor())

  # 模糊:

  image.filter(ImageFilter.BLUR)

  # uuid1 生成唯一的字符串作為驗證碼圖片名稱
  code_name = '{}.jpg'.format(uuid1())
  save_dir = './{}'.format(code_name)
  image.save(save_dir, 'jpeg')
  print("已保存圖片: {}".format(save_dir))

# 當(dāng)直接運行文件的是和,運行下面代碼
if __name__ == "__main__":
  create_code()
(venv) allenwoo@~/renren/code$ python test.py 
保存驗證碼 ef3k 到數(shù)據(jù)庫
已保存圖片: ./c86e03c0-1c23-11e7-999d-f45c89c09e61.jpg
(venv) allenwoo@~/renren/code$ python test.py 
保存驗證碼 I37X 到數(shù)據(jù)庫
已保存圖片: ./cb8aed02-1c23-11e7-9b18-f45c89c09e61.jpg
(venv) allenwoo@~/renren/code$ python test.py 
保存驗證碼 vVL1 到數(shù)據(jù)庫
已保存圖片: ./cc120da8-1c23-11e7-b762-f45c89c09e61.jpg
(venv) allenwoo@~/renren/code$ python test.py 
保存驗證碼 K6w3 到數(shù)據(jù)庫
已保存圖片: ./cc891e05-1c23-11e7-b7ec-f45c89c09e61.jpg




你覺得難不難呢?最后這個生成驗證碼代碼中有些邏輯問題要理解下

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

相關(guān)文章

  • Python Pandas常用函數(shù)方法總結(jié)

    Python Pandas常用函數(shù)方法總結(jié)

    今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著Pandas常用函數(shù)方法展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 基于Python實現(xiàn)微博抓取GUI程序

    基于Python實現(xiàn)微博抓取GUI程序

    在前面的分享中,我們制作了一個天眼查 GUI 程序,今天我們在這個的基礎(chǔ)上,繼續(xù)開發(fā)新的功能,微博抓取工具,感興趣的可以了解一下
    2022-09-09
  • Pytorch搭建YoloV5目標(biāo)檢測平臺實現(xiàn)過程

    Pytorch搭建YoloV5目標(biāo)檢測平臺實現(xiàn)過程

    這篇文章主要為大家介紹了Pytorch搭建YoloV5目標(biāo)檢測平臺實現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04
  • Python基礎(chǔ)入門之魔法方法與異常處理

    Python基礎(chǔ)入門之魔法方法與異常處理

    在python中,所有以“__"雙下劃線包起來的方法,都統(tǒng)稱為魔法方法,下面這篇文章主要給大家介紹了關(guān)于Python基礎(chǔ)入門之魔法方法與異常處理的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • python實現(xiàn)微信小程序用戶登錄、模板推送

    python實現(xiàn)微信小程序用戶登錄、模板推送

    這篇文章主要為大家詳細介紹了python實現(xiàn)微信小程序用戶登錄、模板推送,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 全面了解python中的類,對象,方法,屬性

    全面了解python中的類,對象,方法,屬性

    下面小編就為大家?guī)硪黄媪私鈖ython中的類,對象,方法,屬性。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Python解壓可迭代對象賦值給多個變量詳解

    Python解壓可迭代對象賦值給多個變量詳解

    這篇文章主要為大家介紹了Python賦值多個變量,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • 使用SimpleITK讀取NII格式三維圖像及注意事項說明

    使用SimpleITK讀取NII格式三維圖像及注意事項說明

    這篇文章主要介紹了使用SimpleITK讀取NII格式三維圖像及注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 最新評論