根据请求在烧瓶中显示图像?

时间:2021-03-26 11:40:06

标签: python flask python-requests

我想在将带有图片的 POST 请求发送到我的服务器后在 url 中显示图片,并且每次有新请求到来时它都会更改图片。

我的服务器:

app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static/uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/show_pic',methods=['POST'])
def show_picture():
    if request.method == 'POST':
        file = request.files['image']
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return render_template("show_pic.html", user_img = filename)

我的show_pic.html

<!DOCTYPE html>
<html>
<head>
    <title>show_pic</title>
</head>
<body>
    <img src=" {{url_for('uploads',user_img=filename)}}">
</body>
</html>

我的要求:

import requests
pic={'image': open(r'C:\Users\winwo\OneDrive\Documents\testpic.jpg', mode='rb')}
r = requests.post(url, files=pic)

这是我得到的错误:

2021-03-26T09:47:52.511166+00:00 app[web.1]: [2021-03-26 09:47:52,509] ERROR in app: Exception on /show_pic [POST]
2021-03-26T09:47:52.511203+00:00 app[web.1]: Traceback (most recent call last):
2021-03-26T09:47:52.511204+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
2021-03-26T09:47:52.511204+00:00 app[web.1]:     response = self.full_dispatch_request()
2021-03-26T09:47:52.511205+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
2021-03-26T09:47:52.511205+00:00 app[web.1]:     rv = self.handle_user_exception(e)
2021-03-26T09:47:52.511206+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
2021-03-26T09:47:52.511206+00:00 app[web.1]:     reraise(exc_type, exc_value, tb)
2021-03-26T09:47:52.511206+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
2021-03-26T09:47:52.511207+00:00 app[web.1]:     raise value
2021-03-26T09:47:52.511207+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
2021-03-26T09:47:52.511207+00:00 app[web.1]:     rv = self.dispatch_request()
2021-03-26T09:47:52.511207+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
2021-03-26T09:47:52.511208+00:00 app[web.1]:     return self.view_functions[rule.endpoint](**req.view_args)
2021-03-26T09:47:52.511208+00:00 app[web.1]:   File "/app/app_with_handler.py", line 121, in show_picture
2021-03-26T09:47:52.511209+00:00 app[web.1]:     file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
2021-03-26T09:47:52.511209+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/datastructures.py", line 3066, in save
2021-03-26T09:47:52.511209+00:00 app[web.1]:     dst = open(dst, "wb")
2021-03-26T09:47:52.511214+00:00 app[web.1]: FileNotFoundError: [Errno 2] No such file or directory: '/app/static/uploads/testpic.jpg'
2021-03-26T09:47:52.512268+00:00 app[web.1]: 10.138.162.166 - - [26/Mar/2021:09:47:52 +0000] "POST /show_pic HTTP/1.1" 500 290 "-" "python-requests/2.25.1"

在上面的代码中,我尝试读取图片并将其保存到uploads文件夹。但如果有一种方法可以读取和显示图片而无需将其保存到文件夹中,那也适用于我。

1 个答案:

答案 0 :(得分:1)

有一种方法可以在 Flask 上渲染图像文件,而无需将它们实际保存在您的机器上。

尝试使用 data uri

可以在 this 帖子中找到关于此的更多信息。