最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Flask?web上傳獲取圖像Image讀取并使用方式

 更新時(shí)間:2022年11月28日 10:01:35   作者:loong_XL  
這篇文章主要介紹了Flask?web上傳獲取圖像Image讀取并使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Flask web上傳獲取圖像Image讀取并使用

圖片上傳界面

后端

@app.route('/upload')
def upload_test():
    return render_template('new.html')

前端:new.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
    <body>
        <div>
            <form method="post" action="http://localhost:6600/up_photo" enctype="multipart/form-data">
            <input type="file" size="30" name="photo"/>
            <br>
<!--            <input type="text" class="txt_input" name="name" style="margin-top:15px;"/>-->
            <input type="submit" value="提交信息" class="button-new" style="margin-top:15px;"/>
            </form>
        </div>
    </body>
</html>

在這里插入圖片描述

圖片上傳后端處理代碼

后端

***stream獲取圖像文件,另外[‘photo’]與前端name="photo"屬性對(duì)其

@app.route('/up_photo', methods=['post'])
def up_photo():
    img1 = request.files['photo']
    print(type(img1))

    img = Image.open(img1.stream)


	# 保存圖片
    img1.save(file_path)

Flask上傳本地圖片并在頁面上顯示

使用Flask遠(yuǎn)程上傳圖片到服務(wù)器,并把獲取到的圖片顯示到前端頁面上。

方法一

目錄結(jié)構(gòu):

  • 'static/images' 文件夾用來存放上傳過來的圖片
  • 'templates’文件夾下的兩個(gè)html文件定義顯示頁面
  • upload_pictures.py 是工程代碼

'static/images' 文件夾用來存放上傳過來的圖片‘templates’文件夾下的兩個(gè)html文件定義顯示頁面upload_pictures.py 是工程代碼

upload_pictures.py 代碼:

# coding:utf-8
 
from flask import Flask, render_template, request, redirect, url_for, make_response,jsonify
from werkzeug.utils import secure_filename
import os
import cv2
import time
 
from datetime import timedelta
 
#設(shè)置允許的文件格式
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'JPG', 'PNG', 'bmp'])
 
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
 
app = Flask(__name__)
# 設(shè)置靜態(tài)文件緩存過期時(shí)間
app.send_file_max_age_default = timedelta(seconds=1)
 
 
# @app.route('/upload', methods=['POST', 'GET'])
@app.route('/upload', methods=['POST', 'GET'])  # 添加路由
def upload():
    if request.method == 'POST':
        f = request.files['file']
 
        if not (f and allowed_file(f.filename)):
            return jsonify({"error": 1001, "msg": "請(qǐng)檢查上傳的圖片類型,僅限于png、PNG、jpg、JPG、bmp"})
 
        user_input = request.form.get("name")
 
        basepath = os.path.dirname(__file__)  # 當(dāng)前文件所在路徑
 
        upload_path = os.path.join(basepath, 'static/images', secure_filename(f.filename))  # 注意:沒有的文件夾一定要先創(chuàng)建,不然會(huì)提示沒有該路徑
        # upload_path = os.path.join(basepath, 'static/images','test.jpg')  #注意:沒有的文件夾一定要先創(chuàng)建,不然會(huì)提示沒有該路徑
        f.save(upload_path)
 
        # 使用Opencv轉(zhuǎn)換一下圖片格式和名稱
        img = cv2.imread(upload_path)
        cv2.imwrite(os.path.join(basepath, 'static/images', 'test.jpg'), img)
 
        return render_template('upload_ok.html',userinput=user_input,val1=time.time())
 
    return render_template('upload.html')
 
 
if __name__ == '__main__':
    # app.debug = True
    app.run(host='0.0.0.0', port=8987, debug=True)

upload.html 文件代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flask上傳圖片演示</title>
</head>
<body>
    <h1>使用Flask上傳本地圖片并顯示示例一</h1>
    <form action="" enctype='multipart/form-data' method='POST'>
        <input type="file" name="file" style="margin-top:20px;"/>
        <br>
        <i>請(qǐng)輸入你當(dāng)前的心情(開心、超開心、超超開心):</i>
        <input type="text" class="txt_input" name="name"  value="超超開心" style="margin-top:10px;"/>
        <input type="submit" value="上傳" class="button-new" style="margin-top:15px;"/>
    </form>
</body>
</html>

upload_ok.html文件代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flask上傳圖片演示</title>
</head>
<body>
    <h1>使用Flask上傳本地圖片并顯示示例一</h1>
    <form action="" enctype='multipart/form-data' method='POST'>
        <input type="file" name="file" style="margin-top:20px;"/>
        <br>
        <i>請(qǐng)輸入你當(dāng)前的心情(開心、超開心、超超開心):</i>
        <input type="text" class="txt_input" name="name"  value="超超開心" style="margin-top:10px;"/>
        <input type="submit" value="上傳" class="button-new" style="margin-top:15px;"/>
    </form>
    <h1>閣下的心情是:{{userinput}}!</h1>
    <img src="{{ url_for('static', filename= './images/test.jpg',_t=val1) }}" width="400" height="400" alt="你的圖片被外星人劫持了~~"/>
</body>
</html>

直接運(yùn)行 python upload_pictures.py 就行了,定義了端口號(hào)8987,在本機(jī)上訪問 '127.0.0.1:8987/upload' ,出現(xiàn)以下界面:

點(diǎn)擊'瀏覽' 并上傳后,上傳的圖片保存到了 ‘static/images'目錄下,顯示結(jié)果:

方法二

目錄結(jié)構(gòu):

目錄文件介紹同方法一。

upload_pictures.py 代碼:

# coding:utf-8
 
from flask import Flask,render_template,request,redirect,url_for,make_response,jsonify
from werkzeug.utils import secure_filename
import os
import cv2
 
from datetime import timedelta
 
#設(shè)置允許的文件格式
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'JPG', 'PNG', 'bmp'])
 
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
 
app = Flask(__name__)
# 設(shè)置靜態(tài)文件緩存過期時(shí)間
app.send_file_max_age_default = timedelta(seconds=1)
 
@app.route('/upload', methods=['POST', 'GET'])  # 添加路由
def upload():
    if request.method == 'POST':
        f = request.files['file']
 
        if not (f and allowed_file(f.filename)):
            return jsonify({"error": 1001, "msg": "請(qǐng)檢查上傳的圖片類型,僅限于png、PNG、jpg、JPG、bmp"})
 
        user_input = request.form.get("name")
 
        basepath = os.path.dirname(__file__)  # 當(dāng)前文件所在路徑
 
        upload_path = os.path.join(basepath, 'static/images',secure_filename(f.filename))  #注意:沒有的文件夾一定要先創(chuàng)建,不然會(huì)提示沒有該路徑
        f.save(upload_path)
 
        image_data = open(upload_path, "rb").read()
        response = make_response(image_data)
        response.headers['Content-Type'] = 'image/png'
        return response
 
    return render_template('upload.html')
 
if __name__ == '__main__':
    # app.debug = True
    app.run(host = '0.0.0.0',port = 8987,debug= True)

upload.html 文件代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flask上傳圖片演示</title>
</head>
<body>
    <h1>使用Flask上傳本地圖片并顯示示例二</h1>
    <form action="" enctype='multipart/form-data' method='POST'>
        <input type="file" name="file" style="margin-top:20px;"/>
        <br>
        <i>請(qǐng)輸入你當(dāng)前的心情(開心、超開心、超超開心):</i>
        <input type="text" class="txt_input" name="name"  value="超超開心" style="margin-top:10px;"/>
        <input type="submit" value="上傳" class="button-new" style="margin-top:15px;"/>
    </form>
</body>
</html>

運(yùn)行 python upload_pictures.py ,端口號(hào)定義的是8987,在本機(jī)上訪問 '127.0.0.1:8987/upload' ,出現(xiàn)以下界面:

點(diǎn)擊'瀏覽' 并上傳后,上傳的圖片保存到了 ‘static/images'目錄下,顯示結(jié)果:

方法二顯示出來的圖片覆蓋了整個(gè)頁面。

tips: 如果是在其他機(jī)器上訪問,把127.0.0.1的IP換成服務(wù)器的IP就行了。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 在django中查詢獲取數(shù)據(jù),get, filter,all(),values()操作

    在django中查詢獲取數(shù)據(jù),get, filter,all(),values()操作

    這篇文章主要介紹了在django中查詢獲取數(shù)據(jù),get, filter,all(),values()操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 在Django中管理Users和Permissions以及Groups的方法

    在Django中管理Users和Permissions以及Groups的方法

    這篇文章主要介紹了在Django中管理Users和Permissions以及Groups的方法,Django是最具人氣的Python web開發(fā)框架,需要的朋友可以參考下
    2015-07-07
  • python接口自動(dòng)化如何封裝獲取常量的類

    python接口自動(dòng)化如何封裝獲取常量的類

    這篇文章主要介紹了python接口自動(dòng)化如何封裝獲取常量的類,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • python中的opencv?圖像梯度

    python中的opencv?圖像梯度

    這篇文章主要介紹了python中的opencv?圖像梯度,圖像梯度計(jì)算的是圖像變化的速度,圖像梯度計(jì)算需要求導(dǎo)數(shù),但是圖像梯度一般通過計(jì)算像素值的差來得到梯度的近似值,下文詳細(xì)介紹需要的小伙伴可以參考一下
    2022-06-06
  • Python使用Matplotlib進(jìn)行圖案填充和邊緣顏色分離的三種方法

    Python使用Matplotlib進(jìn)行圖案填充和邊緣顏色分離的三種方法

    Matplotlib是Python中功能強(qiáng)大的繪圖庫,允許廣泛的自定義選項(xiàng),一個(gè)常見的要求是分離出圖中的圖案填充和邊緣顏色,默認(rèn)情況下,Matplotlib中的填充顏色與邊緣顏色相關(guān)聯(lián),但有一些方法可以獨(dú)立自定義這些顏色,本文將深入研究如何實(shí)現(xiàn)這一點(diǎn)的技術(shù)細(xì)節(jié),并提供分步說明和示例
    2025-01-01
  • 基于PyQt5完成pdf轉(zhuǎn)word功能

    基于PyQt5完成pdf轉(zhuǎn)word功能

    本文介紹的pdf轉(zhuǎn)word功能還有一些待完善地方,例如可增加預(yù)覽功能,實(shí)現(xiàn)每頁預(yù)覽,當(dāng)然我們可以在后續(xù)階段逐漸完善,對(duì)基于PyQt5完成的pdf轉(zhuǎn)word功能感興趣的朋友一起看看吧
    2022-06-06
  • 詳解Python3定時(shí)器任務(wù)代碼

    詳解Python3定時(shí)器任務(wù)代碼

    這篇文章主要介紹了Python3定時(shí)器任務(wù)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 控制Python浮點(diǎn)數(shù)輸出位數(shù)的操作方法

    控制Python浮點(diǎn)數(shù)輸出位數(shù)的操作方法

    在python的輸出結(jié)果中,尤其是浮點(diǎn)數(shù)的輸出,當(dāng)我們需要寫入文本文件時(shí),最好是采用統(tǒng)一的輸出格式,這樣也能夠增強(qiáng)結(jié)果的可讀性,這篇文章主要介紹了控制Python浮點(diǎn)數(shù)輸出位數(shù)的方法,需要的朋友可以參考下
    2022-04-04
  • Python通過類的組合模擬街道紅綠燈

    Python通過類的組合模擬街道紅綠燈

    這篇文章主要介紹了Python通過類的組合模擬街道紅綠燈,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • PyQt5 對(duì)圖片進(jìn)行縮放的實(shí)例

    PyQt5 對(duì)圖片進(jìn)行縮放的實(shí)例

    今天小編就為大家分享一篇PyQt5 對(duì)圖片進(jìn)行縮放的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06

最新評(píng)論

舒兰市| 云安县| 黑山县| 广安市| 梁河县| 兴化市| 河池市| 新津县| 阿合奇县| 容城县| 浦县| 湟源县| 化州市| 灵寿县| 平顺县| 汪清县| 漳浦县| 绥棱县| 普宁市| 永丰县| 北安市| 望城县| 竹溪县| 崇礼县| 隆昌县| 虞城县| 奉新县| 锡林浩特市| 瑞金市| 长岭县| 永登县| 江津市| 南通市| 唐海县| 禹城市| 塔河县| 咸阳市| 勃利县| 南江县| 海丰县| 南岸区|