python 制作验证码

安装python
https://www.runoob.com/python/python-install.html
安装图形工具

pip install pillow

image.png

可能遇到的问题
'pip' 不是内部或外部命令,也不是可运行的程序或批处理文件。
https://blog.csdn.net/betterfate/article/details/71123976

img.py

#pip install pillow
from PIL import Image, ImageDraw, ImageFont
from random import randint
import sys


W=int(sys.argv[1])
H=int(sys.argv[2])
FS=20
s=sys.argv[3]

img=Image.new('RGB', (W, H), (255,255,255))
gd=ImageDraw.Draw(img)

for i in range(len(s)):
    gd.text(
        (i*W/len(s)+randint(-5,5), (H-FS)/2+randint(-5,5)),
        s[i],
        (0,0,0),
        ImageFont.truetype("C:\\Windows\\Fonts\\ariblk.ttf", FS)
)

for i in range(10):
    gd.line(
        (randint(0,W), randint(0,H), randint(0,W), randint(0,H)),
        fill=(randint(0,255),randint(0,255),randint(0,255)),
        width=1
    )

for i in range(100):
    gd.point(
        (randint(0,W), randint(0,H)), fill=(randint(0,255),randint(0,255),randint(0,255))
    )


img.save('img.png', "PNG")

执行命令


image.png

生成图片


image.png

你可能感兴趣的:(python 制作验证码)