我有一個 HTML 頁面,可讓您上傳圖片并顯示它(非常簡單)。但是如何將特定加載的影像發送到我的燒瓶應用程式中,以便我可以使用 python 的 openCV 或 PIL 包對其執行一些影像處理?
uj5u.com熱心網友回復:
免責宣告
我最近做過類似的事情,它可能不是你的 100% 解決方案,但使用這段代碼的一部分可以解決你所有的問題。
燒瓶代碼
這部分獲取從瀏覽器上傳的所有檔案,并使用其尊重的擴展名保存它們。
if request.method=="PUT":
for f in request.files.getlist("Files"): #<---- note 1
f.save(os.path.join(Path_To_Folder, f.filename))
return jsonify({"result": "res"}), 200 #status code is only thing important here
HTML
這僅允許您從設備中選擇檔案
<input type="file" id="file_loader" multiple/>
JavaScript
這段代碼
const file_loader = document.getElementById("file_loader");
file_loader.onchange = async function(e){
alert("Upload started")
let sending = new FormData(); //creates form data object
//this for loop adds all files you selected to a form object
for (let i = 0; i < file_loader.files.length; i ) {
sending.append("Files", file_loader.files[i]); //<---- note 1
}
//this part just sends all the files to server
const podaci = await fetch(url_filesistem, {
method: "PUT",
body: sending,
})
.then((response) => response.json())
.then((pod) => {return pod;});
//removing all selected files from an input, so every time you want to select files
//its just those files, not ones previously selected
while (file_loader.length > 0) {
file_loader.pop();
}
alert("Finnished uploading")
}
注1
兩行中提到的字串“檔案”必須相同才能使此方法起作用。
建議
首先將所有檔案保存在服務器上,然后進行處理。我不知道python中for回圈中的“f”物件包含什么,因此我不知道您是否可以立即處理它們。
隨時問我有關我的代碼的任何問題!
此致!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/487858.html
