Python 模擬生成動態(tài)產(chǎn)生驗(yàn)證碼圖片的方法
模擬動態(tài)產(chǎn)生驗(yàn)證碼圖片
模擬生成驗(yàn)證碼,首先要做的是生成隨機(jī)的字母,然后對字母進(jìn)行模糊處理。這里介紹一下 Python 提供的 Pillow 模塊。
Pillow
PIL:Python Image Library,Python 的圖像處理標(biāo)準(zhǔn)庫,功能強(qiáng)大。
PIL 是第三方庫,使用之前需要先進(jìn)行安裝。具體的命令如下:(如果安裝了 Anaconda,這一步可以跳過)
$ pip install pillow
下面先簡單介紹 Pillow 的功能。
操作圖像
縮放圖像,是 Pillow 的一個功能,示例如下:
from PIL import Image
# 打開圖片,注意路徑
img = Image.open('pitbull.jpeg')
# 獲得圖片尺寸
weight, height = img.size
print('原圖片尺寸:{}x{}'.format(weight, height))
# 進(jìn)行縮放,縮放 50%
img.thumbnail((weight//2, height//2))
print('調(diào)整后的圖片尺寸:{}x{}'.format(weight//2, height//2))
# 將縮放后的圖片保存
img.save('thumbnail.jpg', 'jpeg')
Pillow 還有其他的功能,例如旋轉(zhuǎn),剪切,濾鏡,輸出文字,調(diào)色板等。
ImageFilter
下面嘗試模糊圖片處理:
from PIL import Image,ImageFilter
# 打開圖片文件,注意路徑
img = Image.open('pitbull.jpeg')
# 應(yīng)用模糊濾鏡
img2 = img.filter(ImageFilter.BLUR)
img2.save('blur.jpg', 'jpeg')
ImageFilter 是 Python 提供的圖像濾波,而 ImageFilter.BLUR 是模糊濾波。
上面代碼具體的效果如下:


ImageDraw
同時 Pillow 的 ImageDraw 提供了一些列繪圖方法,使我們可以直接繪圖。下面使用這種方法來嘗試生成字母驗(yàn)證碼圖片:
# -*- coding: utf-8 -*-
'''
@File: generate_random_code.py
@Time: 2020/01/31 20:32:10
@Author: 大夢三千秋
@Contact: yiluolion@126.com
'''
# Put the import lib here
from random import randint, choice
from PIL import Image, ImageDraw, ImageFont, ImageFilter
def rnd_chr(chr_set):
'''獲取隨機(jī)字符
Args:
chr_set: 擬定生成的字符集
Returns:
返回隨機(jī)字符
'''
return choice(chr_set)
def rnd_bg_color():
'''獲取隨機(jī)像素值,填充背景
Returns:
返回隨機(jī)像素值,返回元組類型
'''
return (randint(97, 255), randint(97, 255), randint(97, 255))
def rnd_chr_color():
'''獲取隨機(jī)像素,填充輸出字符
Returns:
返回隨機(jī)像素值,返回元組類型
'''
# 與畫板填充色進(jìn)行一定的錯開,防止完全覆蓋
return (randint(32, 96), randint(32, 96), randint(32, 96))
def main():
# 生成字符集
chr_set = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
for i in range(65, 91):
chr_set.append(chr(i))
for j in range(97, 123):
chr_set.append(chr(j))
# print(chr_set)
# 定義畫板規(guī)格 250 x 50
width = 60 * 5
height = 60
# 創(chuàng)建 Image 對象,白底畫板
image = Image.new('RGB', (width, height), (255, 255, 255))
# 創(chuàng)建 Draw 對象
draw = ImageDraw.Draw(image)
# 創(chuàng)建 Font 對象
font = ImageFont.truetype('./consola.ttf', 36)
# 填充畫板
for x in range(width):
for y in range(height):
draw.point((x, y), fill=rnd_bg_color())
# 填充文字
for n in range(5):
draw.text((60 * n + 25, 12), rnd_chr(chr_set), fill=rnd_chr_color(), font=font)
# 對圖像內(nèi)容進(jìn)行模糊后存儲
image = image.filter(ImageFilter.BLUR)
image.save('./random_code.jpg', 'jpeg')
if __name__ == "__main__":
main()
代碼具體實(shí)現(xiàn)過程是,先用隨機(jī)顏色填充背景,再生成字母,最后對圖片進(jìn)行模糊處理。具體實(shí)現(xiàn)效果如下:

代碼在運(yùn)行的過程中,可能會出現(xiàn)下面的錯誤:
IOError: cannot open resourse
出現(xiàn)這個問題的原因是 PIL 無法定位到字體文件的位置,可以考慮直接提供絕對位置。
font = ImageFont.truetype('/absolute/path/font.ttf', 36)
本篇文章提及的 Pillow 僅是簡單的部分功能,如果要繼續(xù)了解 PIL 的強(qiáng)大功能,可以參考 Pillow 官方文檔:
https://pillow.readthedocs.org/
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python3定位并識別圖片驗(yàn)證碼實(shí)現(xiàn)自動登錄功能
- python繞過圖片滑動驗(yàn)證碼實(shí)現(xiàn)爬取PTA所有題目功能 附源碼
- Python利用Pillow(PIL)庫實(shí)現(xiàn)驗(yàn)證碼圖片的全過程
- python 識別登錄驗(yàn)證碼圖片功能的實(shí)現(xiàn)代碼(完整代碼)
- python圖片驗(yàn)證碼識別最新模塊muggle_ocr的示例代碼
- Python基于內(nèi)置庫pytesseract實(shí)現(xiàn)圖片驗(yàn)證碼識別功能
- python識別驗(yàn)證碼圖片實(shí)例詳解
- Python +Selenium解決圖片驗(yàn)證碼登錄或注冊問題(推薦)
- Python 識別12306圖片驗(yàn)證碼物品的實(shí)現(xiàn)示例
- python爬蟲如何解決圖片驗(yàn)證碼
相關(guān)文章
從零學(xué)python系列之?dāng)?shù)據(jù)處理編程實(shí)例(一)
本文目的:用一個實(shí)例總結(jié)學(xué)習(xí)到的with語句,函數(shù),列表推導(dǎo),集合,排序,字符分割等內(nèi)容2014-05-05
Python 實(shí)現(xiàn)數(shù)據(jù)庫更新腳本的生成方法
下面小編就為大家?guī)硪黄狿ython 實(shí)現(xiàn)數(shù)據(jù)庫更新腳本的生成方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
Python數(shù)據(jù)挖掘中常用的五種AutoEDA 工具總結(jié)
大家好,我們都知道在數(shù)據(jù)挖掘的過程中,數(shù)據(jù)探索性分析一直是非常耗時的一個環(huán)節(jié),但也是繞不開的一個環(huán)節(jié),本篇文章帶你盤點(diǎn)數(shù)據(jù)挖掘中常見的5種 AutoEDA 工具2021-11-11

