我正在尝试获取图像输入并对其进行一些处理,然后再在网页上显示输出。但是,当我尝试使用不同的图像时,显示的是旧图像而不是新图像。
以下是代码片段:
from flask import Flask, render_template, request
import pro
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/process', methods=['GET', 'POST'])
def process():
n = '/home/vivek/Desktop/CL3/d/booth/trial/uploads/up.jpg'
a = pro.pro(n)
return render_template("out.html")
if __name__ == '__main__':
app.run(debug=True)
过程代码:
import cv2
def pro(p):
img = cv2.imread(p, 1)
# some process here
path = '/home/vivek/Desktop/CL3/d/booth/trial/static/'
cv2.imwrite(str(path) + 'out.jpg',img)
cv2.waitKey(0)
np = '/home/vivek/Desktop/CL3/d/booth/trial/static/out.jpg'
return np
最后是 HTML 文件:
<!DOCTYPE html>
<html>
<body>
<h2>OUTPUT</h2>
<img src="{{url_for('static', filename='out.jpg')}}" alt="OUTPUT" style="width:128px;height:128px;">
</body>
</html>
最佳答案
你需要禁用缓存
def process():
....
resp = make_response(render_template('out.html'))
resp.cache_control.no_cache = True
return resp
已更新
class MyFlask(Flask):
def get_send_file_max_age(self, name):
if name == 'out.jpg':
return 0
return Flask.get_send_file_max_age(self, name)
app = MyFlask(__name__)
关于python - 如何在静态文件夹中使用 Flask 中的 opencv 显示新保存的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44256826/