首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在图片下方添加标题/句子?

如何在图片下方添加标题/句子?
EN

Stack Overflow用户
提问于 2019-03-04 04:30:45
回答 1查看 0关注 0票数 0

我想把图片放在图片下方,但我看到的只是这张图片。这里

这是我的HTML文件的代码。

<html>
<body>
  <h1 align='center'>GENERATING IMAGE CAPTIONS</h1>
  <center>
  <form action = "" method = "POST" enctype = "multipart/form-data">
    <input type = "file" name = "file">
    <input type = "submit" value="Upload Image">
  </form>
  </center>

  {% if after %}
  <h2 align='center'>{{caption}}</h2>
  <br><center><img src="{{url_for(alert('static', filename=imgname))}}" width="500vw"></center>

  {% endif %}
</body>
</html>

这是我的app文件的代码

import os
from flask import Flask, flash, request, redirect, url_for, render_template
from werkzeug.utils import secure_filename
from flask import send_from_directory
from Yey import *


inception = models.inception_v3(pretrained=True)
flick = ImageCaptionDataset()
net = IC(flick.tokens)
net.load_state_dict(torch.load('models/model.pth'))

UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


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

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']

        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            fname = './uploads/' + secure_filename(file.filename)
            caption = f.print_beam_caption(net, fname, 3)
            print(caption)
            return redirect(url_for('uploaded_file',
                                    filename=filename, after = True, caption = caption, imgname = secure_filename(file.filename)))
    return render_template('upload.html', after = False)

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

if __name__ == '__main__':
    app.run(debug = False)

注意:我从官方文档中获取了代码。

我是新手,并且从来没有编写过HTML / CSS / JS只是python,所以很抱歉,如果我的代码编写得不好。

EN

回答 1

Stack Overflow用户

发布于 2019-03-04 13:42:22

flask_view:

import os
from flask import render_template

@app.route('/uploads/<filename>')
def hello(filename):
    file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
    caption = 'something'
    return render_template('upload.html', file_path=file_path, caption=caption)

HTML文件upload.html:

<html>
<body>
  <h1 align='center'>Uploaded IMAGE</h1>
  <img src="{{file_path}}">
  <p>{{caption}}</p>
</body>
</html>

这样你需要做的事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档