Flask 上传图片并灰度显示
接到了一个小需求:使用flask写上传图片并灰度显示
刚好没写过python的后端,来试一试
代码
使用了layui的组件
app.js
import logging
import os
from flask import Flask, render_template, request
from PIL import Image
app = Flask(__name__)
# 映射到index.html
@app.route('/')
def index():
return render_template('index.html')
# 指定post请求
@app.route('/upload', methods=['post'])
def upload():
# 文件不存在就退出
if ('file' not in request.files):
print('no found file')
return
img = request.files['file']
path = os.path.abspath(os.path.dirname(__file__)) + "/static/photo"
# 日志打印
app.logger.info("path: " + path)
# 创建文件夹
if os.path.exists(path) == False:
os.makedirs(path)
file_path =</