【问题标题】:How to set the path of uploaded photos in flask如何在flask中设置上传照片的路径
【发布时间】:2021-08-18 07:43:48
【问题描述】:

我正在尝试上传图像并使用接收到的图像运行 opencv。 它说文件上传成功,但是我不知道如何设置使用上传图像的路径而不是img部分的绝对路径。 这是文件上传。

@app.route('/', methods = ['GET', 'POST'])
def uploader_file():
   if request.method == 'POST':
      f = request.files['file']
      f.save(secure_filename(f.filename))
      return redirect(url_for('test'))

这是html

 <html>
      <head>
        <title>얼굴황금비율인식AI</title>
      </head>
      <body>
        <h1>얼굴황금비율인식 AI</h1>
        <img src="{{ url_for('video_feed') }}">
        <h2>사진을 업로드 해주세요</h2>
        <form action = "" method= "POST"
          enctype = "multipart/form-data">
          <input type = "file" name = "file"/>
          <input type = "submit"/>
        </form>
      </body>
    </html>

不知道如何设置接收文件的图片路径。

 img = "C:\\Users\\20khj\\Desktop\\photo.jpg"
 img = cv2.imread("C:\\Users\\20khj\\Desktop\\photo.jpg")

【问题讨论】:

    标签: python flask image-upload


    【解决方案1】:

    你可以试试这个来保存图片:

    @app.route('/', methods = ['GET', 'POST'])
    def uploader_file():
       if request.method == 'POST':
          f = request.files['file']
          f.save("/static/"+f.filename) 
          # You may write that f.filename by yourself to save in any name as you want : 
          f.save("/static/"+"photo.jpg")
          return redirect(url_for('uploader_file'))
    

    并获得该图像:

    @app.route("/video_feed")
    def video_feed():
        return redirect("/static/photo.jpg")
    

    【讨论】:

    • 上传文件后成功保存在static\image文件中。但是这里,如何使用上传的照片而不是img变量中的固定路径在opencv中使用呢?
    • 我没听清楚。你想用opencv保存文件吗?
    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 2017-11-02
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多