pillow模块
制作验证码
- 编写返回随机颜色与随机字符的函数
- 创建图片画布
- 创建文字
- 创建ImageDraw.Draw对象
- 填充随机色块底色
- 填充文字
- 模糊
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import random
# 随机字母:
def rndChar():
return chr(random.randint(65, 90))
# 随机颜色1:
def rndColor():
return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))
# 随机颜色2:
def rndColor2():
return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))
# 240 x 60:
width = 200 * 4
height = 200
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建Font对象:
font = ImageFont.truetype(r"H:\APP_data\学习\CODE学习\Python\abc\black.ttf", 140)
# 创建Draw对象:
draw = ImageDraw.Draw(image)
# 填充每个像素:
for x in range(width):
for y in range(height):
draw.point((x, y), fill=rndColor())
# 输出文字:
for t in range(4):
draw.text((140 * t + 120, 25), rndChar(), font=font, fill=rndColor2())
# 模糊:
image = image.filter(ImageFilter.BLUR)
image.save('code.jpg', 'jpeg')
requests
chardet
根据搜索同时安装chardet
与bs4
并使用如下准确性更高 :
from bs4 import UnicodeDammit
data = '离离原上草,一岁一枯荣'.encode('utf-8')
dammit = UnicodeDammit(data)
print(dammit.unicode_markup)
print(dammit.detector.chardet_encoding)
# 输出
离离原上草,一岁一枯荣
utf-8
图形界面开始
阅读量
loading...