Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask,我如何回应页面上的图片_Python_Flask - Fatal编程技术网

Python Flask,我如何回应页面上的图片

Python Flask,我如何回应页面上的图片,python,flask,Python,Flask,我是Flask和web开发的新手,我想上传一张图片,通过我的深度学习应用程序进行处理,然后在页面上响应处理后的图片,这是我的框架代码 # coding: utf-8 import os import uuid import PIL.Image as Image from werkzeug import secure_filename from flask import Flask, url_for, render_template, request, url_for, redirect, se

我是Flask和web开发的新手,我想上传一张图片,通过我的深度学习应用程序进行处理,然后在页面上响应处理后的图片,这是我的框架代码

# coding: utf-8
import os
import uuid

import PIL.Image as Image
from werkzeug import secure_filename
from flask import Flask, url_for, render_template, request, url_for, redirect, send_from_directory


ALLOWED_EXTENSIONS = set(list(['png', 'jpg', 'jpeg']))
UPLOAD_FOLDER = '/tmp'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


def process_image(file_path):
    """
    resize image to 32x32
    :param file_path: file path
    :return:
    """
    img = Image.open(file_path, mode='r')
    return img.resize([32,32], Image.ANTIALIAS)


def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


@app.route('/', methods=['GET', 'POST'])
def upload_file():
    _path = None
    if request.method == 'POST':
        _file = request.files['file']
        print(_file)
        if _file and allowed_file(_file.filename):
            filename = secure_filename(_file.filename)
            _path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            _file.save(_path)
            return show_pic(deep_learning(_path))

    return '''
            <!DOCTYPE html>
            <title>Web App/title>
            <h1>Deep Learning Web App</h1>
            <form action="/" method="POST" enctype="multipart/form-data">
            <input type="file" name="file" />
            <input type="submit" value="submit" />
            </form>
            '''


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

if __name__ == '__main__':
    app.run()
#编码:utf-8
导入操作系统
导入uuid
将PIL.Image作为图像导入
从werkzeug导入安全文件名
从flask导入flask、url\u for、呈现\u模板、请求、url\u for、重定向、从\u目录发送\u
允许的扩展名=set(列表(['png','jpg','jpeg']))
上传文件夹='/tmp'
app=烧瓶(名称)
app.config['UPLOAD\u FOLDER']=UPLOAD\u FOLDER
def进程图像(文件路径):
"""
将图像大小调整为32x32
:param file_path:文件路径
:返回:
"""
img=Image.open(文件路径,mode='r')
返回img.resize([32,32],Image.ANTIALIAS)
允许的def_文件(文件名):
在文件名中返回“.”,在允许的扩展名中返回filename.rsplit(“.”,1)[1]
@app.route('/',方法=['GET','POST'])
def upload_文件():
_路径=无
如果request.method==“POST”:
_file=request.files['file']
打印(_文件)
如果\u文件和允许的\u文件(\u file.filename):
filename=secure\u filename(\u file.filename)
_path=os.path.join(app.config['UPLOAD\u FOLDER'],文件名)
_file.save(_路径)
返回显示图片(深度学习(路径))
返回“”'
Web应用程序/标题>
深度学习Web应用程序
'''
@app.route(“/uploads/”)
def上传文件(文件名):
从_目录返回发送_(app.config['UPLOAD_FOLDER'],文件名)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run()
正如您所看到的,我已经实现了上传图片功能和函数
deep\u learning(path)
,并且它返回了处理过的图片的路径,我需要实现函数
show\u pic()
,我怎么做呢?

用html框架创建一个链接,并将图像路径传递给
render\u template()
函数

result.html

为此,您的文件需要位于
静态
目录或子目录中。

或者您可以使用表单标记下方的
值定义_文件(已处理文件)检查文件是否为无,然后显示:

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    _path = None 
    _file = None
    if request.method == 'POST':
        _file = request.files['file']
        print(_file)
        if _file and allowed_file(_file.filename):
            filename = secure_filename(_file.filename)
            _path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            _file.save(_path)

    return '''
            <!DOCTYPE html>
            <title>Web App/title>
            <h1>Deep Learning Web App</h1>
            <form ...> 
             ...
            </form>
            {% if _file%}
              <img src="{{url_for('uploaded_file', filename=_file) }}" > 
            {% endif %}
            '''
@app.route('/',方法=['GET','POST'])
def upload_文件():
_路径=无
_文件=无
如果request.method==“POST”:
_file=request.files['file']
打印(_文件)
如果\u文件和允许的\u文件(\u file.filename):
filename=secure\u filename(\u file.filename)
_path=os.path.join(app.config['UPLOAD\u FOLDER'],文件名)
_file.save(_路径)
返回“”'
Web应用程序/标题>
深度学习Web应用程序
...
{%if{u文件%}
{%endif%}
'''

从变量名中删除前导的
。你觉得它们有什么用
allowed_file
只是
返回filename.endswith((“.png”、“.jpg”、“.jpeg”)
return render_template('result.html', image_path=deep_learning(_path))
@app.route('/', methods=['GET', 'POST'])
def upload_file():
    _path = None 
    _file = None
    if request.method == 'POST':
        _file = request.files['file']
        print(_file)
        if _file and allowed_file(_file.filename):
            filename = secure_filename(_file.filename)
            _path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            _file.save(_path)

    return '''
            <!DOCTYPE html>
            <title>Web App/title>
            <h1>Deep Learning Web App</h1>
            <form ...> 
             ...
            </form>
            {% if _file%}
              <img src="{{url_for('uploaded_file', filename=_file) }}" > 
            {% endif %}
            '''