forked from nickliqian/cnn_captcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver_captcha_image.py
70 lines (57 loc) · 1.82 KB
/
webserver_captcha_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: UTF-8 -*-
"""
验证码图片接口,访问`/captcha/1`获得图片
"""
from captcha.image import ImageCaptcha
import os
import random
from flask import Flask, request, jsonify, Response, make_response
import json
import io
# Flask对象
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
with open("conf/captcha_config.json", "r") as f:
config = json.load(f)
# 配置参数
root_dir = config["root_dir"] # 图片储存路径
image_suffix = config["image_suffix"] # 图片储存后缀
characters = config["characters"] # 图片上显示的字符集 # characters = "0123456789abcdefghijklmnopqrstuvwxyz"
count = config["count"] # 生成多少张样本
char_count = config["char_count"] # 图片上的字符数量
# 设置图片高度和宽度
width = config["width"]
height = config["height"]
def response_headers(content):
resp = Response(content)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
def gen_special_img():
# 随机文字
text = ""
for j in range(char_count):
text += random.choice(characters)
print(text)
# 生成img文件
generator = ImageCaptcha(width=width, height=height) # 指定大小
img = generator.generate_image(text) # 生成图片
imgByteArr = io.BytesIO()
img.save(imgByteArr, format='PNG')
imgByteArr = imgByteArr.getvalue()
return imgByteArr
@app.route('/captcha/', methods=['GET'])
def show_photo():
if request.method == 'GET':
image_data = gen_special_img()
response = make_response(image_data)
response.headers['Content-Type'] = 'image/png'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
else:
pass
if __name__ == '__main__':
app.run(
host='0.0.0.0',
port=6100,
debug=True
)