python中cv2.imdecode()與cv2.imencode()的使用小結(jié)
cv2.imencode()函數(shù)與cv2.imdecode()函數(shù)通常用于圖片的編碼與解碼。也可以用于帶中文路徑的圖片讀取,網(wǎng)絡(luò)傳輸中的解碼中。
1、圖片路徑帶中文的讀取和寫入
1.1 讀取
- 用cv2.imread()函數(shù)讀取帶中文路徑的圖片時,會報錯:
import cv2 img_path = "/home/dataset/狗/101.jpg" img = cv2.imread(img_path)
運行這個代碼會報錯,可使用numpy的fromfile函數(shù)將圖片加載成array,然后通過cv2.imdecode解碼:
import numpy as np import cv2 from PIL import Image img_path = "/home/dataset/狗/101.jpg" arr_img = np.fromfile(img_path, dtype=np.uint8) # 將文本或二進制文件中數(shù)據(jù)構(gòu)造成數(shù)組 img = cv2.imdecode(arr_img, cv2.IMREAD_COLOR) # BGR通道,和cv2.imread()讀圖片一樣 # 轉(zhuǎn)成Image對象 # rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # pil_img = Image.fromarray(rgb_img) # 用opencv自帶的顯示函數(shù)顯示圖像 cv2.imshow("opencv imgshow", img) cv2.waitKey() # 用matplotlib.pyplot顯示圖像 from matplotlib import pyplot as plt plt.figure("pylot imgshow") # 圖像窗口名稱 plt.imshow(img) plt.axis('off') # 關(guān)掉坐標軸為 off plt.title('pylot imgshow') # 圖像題目 plt.show()
1.2 寫入
- 用cv2.imwrite()函數(shù)將圖片寫入帶中文路徑用于保存圖片時,也會報錯:
import cv2 save_path = "/home/dataset/狗/101.jpg" cv2.imwrite(save_path, img)
我們可以利用cv2.imencode將圖片編碼到內(nèi)存緩沖區(qū)中,然后利用numpy.tofile方法來寫入文件。
import cv2 import numpy as np img_path = "/home/dataset/狗/101.jpg" arr_img = np.fromfile(img_path, dtype=np.uint8) # 將文本或二進制文件中數(shù)據(jù)構(gòu)造成數(shù)組 img = cv2.imdecode(arr_img, cv2.IMREAD_COLOR) # BGR通道,和cv2.imread()讀圖片一樣 arr_buffer = cv2.imencode('.jpg', img)[1] # 保存為圖片 save_path = "/home/dataset/狗/101_copy.jpg" arr_buffer.tofile(save_path) # 保存到文件 # 保存為txt data_encode = np.array(arr_buffer) str_encode = data_encode.tostring() # 緩存數(shù)據(jù)保存到本地 with open('./img_encode.txt', 'w') as f: f.write(str_encode) f.flush
2、在網(wǎng)絡(luò)中傳輸圖片
用flask寫接口接收圖片,服務(wù)端app.py的接收函數(shù)如下:
from flask import Flask, request, jsonify import numpy as np import cv2 app = Flask(__name__) @app.route("/upload_img", methods=['POST']) def upload_img(): f_obj = request.files.get('file', None) if f_obj is None: return jsonify("沒有接收到圖片") else: img_data = f_obj.read() #nparr = np.fromstring(img_data, np.uint8) nparr = np.frombuffer(img_data, np.uint8) # 兩者均可,建議使用np.frombuffer img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) cv2.imwrite("./received_img.jpg", img) return jsonify("接收圖片成功") if __name__ == "__main__": app.run(host='0.0.0.0', port=9000)
模擬客戶端向upload_img接口發(fā)送圖片:
import requests import time upload_img_url = "http://localhost:9000/upload_img" imgfile = {'file':open('/home/dataset/狗/101.jpg','rb')} start = time.time() r = requests.post(upload_img_url, files=imgfile) end = time.time() running_time = end - start print(f"時間消耗: {running_time:.5f} 秒") print(f"響應(yīng)內(nèi)容:{r.text}")
到此這篇關(guān)于python中cv2.imdecode()與cv2.imencode()的使用小結(jié)的文章就介紹到這了,更多相關(guān)python cv2.imdecode()與cv2.imencode()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python-opencv中的cv2.inRange函數(shù)用法說明
- python-opencv 中值濾波{cv2.medianBlur(src, ksize)}的用法
- python opencv檢測直線 cv2.HoughLinesP的實現(xiàn)
- Python實現(xiàn)Opencv cv2.Canny()邊緣檢測
- python opencv鼠標畫矩形框之cv2.rectangle()函數(shù)
- Python中cv2.Canny() 函數(shù)使用方法
- Python圖片縮放cv2.resize()圖文詳解
- python中cv2.projectPoints的用法小結(jié)
- python?cv2.waitKey()函數(shù)的使用
- python中cv2.imread()和Image.open()的區(qū)別和聯(lián)系詳解
相關(guān)文章
Python實現(xiàn)的人工神經(jīng)網(wǎng)絡(luò)算法示例【基于反向傳播算法】
這篇文章主要介紹了Python實現(xiàn)的人工神經(jīng)網(wǎng)絡(luò)算法,結(jié)合實例形式分析了Python基于反向傳播算法實現(xiàn)的人工神經(jīng)網(wǎng)絡(luò)相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Python使用PyQt5/PySide2編寫一個極簡的音樂播放器功能
這篇文章主要介紹了Python中使用PyQt5/PySide2編寫一個極簡的音樂播放器功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02淺談Keras的Sequential與PyTorch的Sequential的區(qū)別
這篇文章主要介紹了淺談Keras的Sequential與PyTorch的Sequential的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06使用Python進行PowerPoint幻燈片背景設(shè)置
設(shè)置PowerPoint幻燈片背景不僅能夠增強演示文稿的視覺吸引力,還能幫助傳達特定的情感或信息,本文將介紹如何使用Python為PowerPoint幻燈片設(shè)置純色、漸變及圖片背景,有需要的可以參考下2024-11-11Python關(guān)于sys.argv[]的用法及說明
sys.argv[]是Python中用于從程序外部獲取參數(shù)的列表,參數(shù)索引從0開始,0索引代表腳本名稱本身,后續(xù)索引代表傳遞給腳本的參數(shù),通過指定索引可以獲取特定的參數(shù),如sys.argv[1]獲取第一個傳入?yún)?shù),當傳入多個參數(shù)時,可以通過切片或循環(huán)獲取全部參數(shù)2024-09-09citespace數(shù)據(jù)處理:用python對Ref文檔進行去重方式
這篇文章主要介紹了citespace數(shù)據(jù)處理:用python對Ref文檔進行去重方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11