Skip to content

Commit e65ebb8

Browse files
committed
添加国旗头像
1 parent 6172673 commit e65ebb8

5 files changed

Lines changed: 88 additions & 0 deletions

File tree

webargstest.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import re
2+
3+
from flask import Flask, jsonify, request
4+
from webargs import fields
5+
from webargs.flaskparser import use_args
6+
7+
app = Flask("hello")
8+
9+
10+
@app.route("/api/login", methods=["POST"])
11+
@use_args({"username": fields.Str(required=True)},
12+
location="json")
13+
def login(args):
14+
name = args['username']
15+
password = args['password']
16+
return jsonify({"code": 200, "msg": "ok"})
17+
18+
19+
@app.route("/api/login", methods=["POST"])
20+
def login3():
21+
data = request.get_json()
22+
email = data.get("email")
23+
if not email or re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email):
24+
return jsonify({"code": 400, "msg": "参数有误"}), 400
25+
password = data.get("password")
26+
if not password or len(password) < 6:
27+
return jsonify({"code": 400, "msg": "参数有误"}), 400
28+
# 数据库查询
29+
return jsonify({"code": 200, "msg": "ok"})
30+
31+
32+
from webargs import fields, ValidationError
33+
34+
35+
def must_exist_in_db(val):
36+
if val != 1:
37+
raise ValidationError("id not exist")
38+
39+
40+
hello_args = {"name": fields.Str(missing="Friend"),
41+
"id": fields.Integer(required=True, validate=must_exist_in_db)}
42+
43+
44+
@app.route("/", methods=["GET"])
45+
@use_args(hello_args, location="query")
46+
def hello(args):
47+
"""A welcome page."""
48+
return jsonify({"message": "Welcome, {}!".format(args["name"])})
49+
50+
51+
@app.errorhandler(422)
52+
@app.errorhandler(400)
53+
def handle_error(err):
54+
headers = err.data.get("headers", None)
55+
messages = err.data.get("messages", ["Invalid request."])
56+
if headers:
57+
return jsonify({"errors": messages}), err.code, headers
58+
else:
59+
return jsonify({"errors": messages}), 400
60+
61+
62+
#
63+
64+
if __name__ == '__main__':
65+
app.run(port=5000)

国旗头像/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from PIL import Image
2+
3+
"""
4+
注意:
5+
1、运行前请确保安装了python3环境
6+
2、请先安装PIL库,命令是: pip install PIL
7+
3、遇到任何问题可通过关注公众号【Python之禅】获得作者的帮助
8+
"""
9+
10+
11+
def main():
12+
avatar = Image.open("header.jpg") # 加载头像
13+
flag = Image.open("flag.jpg") # 加载国旗
14+
avatar_width, avatar_height = avatar.size # 获取头像宽高
15+
flag_size = (int(avatar_width / 3), int(avatar_height / 3))
16+
flag = flag.resize(flag_size, Image.ANTIALIAS) # 将国旗大小缩放到头像的 1/9
17+
avatar.paste(flag, (avatar_width - flag_size[0], avatar_height - flag_size[1])) # 放置在头像右下角
18+
avatar.show()
19+
avatar.save("new_avatar.jpg") # 保存新头像
20+
21+
22+
if __name__ == '__main__':
23+
main()

国旗头像/flag.jpg

4.36 KB
Loading

国旗头像/header.jpg

16.3 KB
Loading

国旗头像/new_avatar.jpg

21.7 KB
Loading

0 commit comments

Comments
 (0)