import random
from flask import *
from PIL import Image
[color=],
ImageDraw
[color=],
ImageFont
import io
app
[color=]=
[color=]Flask
(__name__)
[color=]@app.route
(
[color=]'/'
)
def
[color=]index
():
[color=]
flag
[color=]=
''
[color=]
for i in
[color=]range
(
[color=]5
):
[color=]
n
[color=]=
random.
[color=]randint
(
[color=]0
[color=],
[color=]9
)
if n
[color=]len
(
[color=]range
(
[color=]5
)):
[color=]
if i
[color=]3
:
[color=]
n
[color=]=
[color=]chr
(random.
[color=]randint
(
[color=]65
[color=],
[color=]90
))
else:
[color=]
n
[color=]=
[color=]chr
(random.
[color=]randint
(
[color=]97
[color=],
[color=]122
))
flag
[color=]+=
[color=]str
(n)
[color=]#
创建一个白色背景的图像
[color=]
image
[color=]=
Image.
[color=]new
(
[color=]'RGB'
[color=],
(
[color=]100
[color=],
[color=]50
)
[color=],
[color=]color
[color=]=
[color=]'white'
)
[color=]#
获取一个绘图对象
[color=]
draw
[color=]=
ImageDraw.
[color=]Draw
(image)
[color=]#
设置字体和字体大小
[color=]
font
[color=]=
ImageFont.
[color=]truetype
(
[color=]"arial.ttf"
[color=],
[color=]27
)
[color=]#
在图像上绘制验证码文本
[color=]
draw.
[color=]text
((
[color=]10
[color=],
[color=]10
)
[color=],
flag
[color=],
[color=]fill
[color=]=
[color=]"black"
[color=],
[color=]font
[color=]=
font)
[color=]#
将图像保存到内存中
[color=]
img_io
[color=]=
io.
[color=]BytesIO
()
image.
[color=]save
(img_io
[color=],
[color=]'PNG'
)
img_io.
[color=]seek
(
[color=]0
)
return
[color=]send_file
(img_io
[color=],
[color=]mimetype
[color=]=
[color=]'image/png'
)
if __name__
[color=]==
[color=]'__main__'
:
[color=]
app.
[color=]run
(
[color=]debug
[color=]=
True
[color=],
[color=]host
[color=]=
[color=]'0.0.0.0'
[color=],
[color=]port
[color=]=
[color=]8700
)