【问题标题】:upload and save image with information上传并保存带有信息的图像
【发布时间】:2017-12-25 11:20:55
【问题描述】:

我正在编写一个烧瓶网络应用程序,我们可以在其中上传图像并将其保存在文件系统中。我还想保存带有少量信息的图像,如城市、拍摄日期等。并包括更多功能,如按日期排序图像、在用户中流行、按城市排序。 如果我将图像保存在文件系统而不是数据库上,我该怎么做?

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


    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.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpg', 'jpeg','gif'])
   def allowed_file(filename):
     return '.' in filename and \
       filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']


  @app.route('/')
    def index():
       return render_template('index.html')



@app.route('/upload', methods=['POST'])
    def upload():
      file = request.files['file']
        if file and allowed_file(file.filename):
          filename = secure_filename(file.filename)
          file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return redirect(url_for('uploaded_file',filename=filename))

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

  if __name__ == '__main__':
     app.run(port=4555)

【问题讨论】:

  • 将代码发布为文本而不是图像,以便能够复制和粘贴

标签: python image flask upload


【解决方案1】:

看起来您想访问 exif 数据。看这个包:https://pypi.python.org/pypi/ExifRead

import exifread
# Open image file for reading (binary mode)
f = open(path_name, 'rb')

# Return Exif tags
tags = exifread.process_file(f)

【讨论】:

  • 我提供的链接告诉你怎么做。我还用相关部分更新了我的答案以获取 exif 标签。
  • 未来我将添加一些功能,例如,用城市名称对我的图像进行排序或在用户中显示前十名图像,最近上传的图像。您认为为每张图片保存数据的正确方法是什么?
猜你喜欢
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 2014-08-09
  • 1970-01-01
相关资源
最近更新 更多