python如何將mat文件轉(zhuǎn)為png
將mat文件轉(zhuǎn)為png
花費(fèi)了很大力氣做這件事,總是出現(xiàn)各種錯(cuò)誤,現(xiàn)在終于解決了
from PIL import Image import matplotlib.pyplot as plt import glob import os import numpy as np import mat73 # 數(shù)據(jù)矩陣轉(zhuǎn)圖片的函數(shù) def MatrixToImage(data): data = data*255 new_im = Image.fromarray(data.astype(np.uint8)) return new_im def mkdir(path): folder = os.path.exists(path) if not folder: # 判斷是否存在文件夾如果不存在則創(chuàng)建為文件夾 os.makedirs(path) # makedirs 創(chuàng)建文件時(shí)如果路徑不存在會(huì)創(chuàng)建這個(gè)路徑 print("--- create new folder... ---") else: print("--- There is this folder! ---") # Get all png files under the input folder input_img_path = glob.glob("I:/CCCC--數(shù)據(jù)集/去噪/dnd_2017/input/*.mat") save_path = "blur13x13/" mkdir(save_path) # 調(diào)用函數(shù) i = 0 for file in input_img_path: file_name = file.split('\\')[-1] try: mat = mat73.loadmat(file) new_name = str(mat.keys()) key_name = list(mat.keys())[-1] key_name = mat[key_name] print(key_name.shape) new_im = MatrixToImage(key_name) plt.imshow(key_name, interpolation='nearest') new_im.save(save_path+'{}.png'.format(file_name)) except Exception as e: pass i = i + 1 print("The", i, "picture is currently being processed") continue
完整代碼如上,只需要修改輸入的mat文件夾路徑即可~
將圖片轉(zhuǎn)換為mat格式
import cv2 import numpy as np import h5py import math import glob import os import scipy.io as io ? def save_to_mat(img,output_name): ? ? new_data_path = os.path.join(os.getcwd(),"matType") ? ? if not os.path.isdir(new_data_path): ? ? ? ? os.mkdir(new_data_path) ? ? npy_data = np.array(img,dtype= "uint16") ? ? np.save(new_data_path+'/{}.npy'.format(output_name),npy_data) ? ? npy_load = np.load(new_data_path+'/{}.npy'.format(output_name)) ? ? io.savemat(new_data_path+'/{}.mat'.format(output_name),{'data':npy_load})
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 稀疏矩陣-sparse 存儲(chǔ)和轉(zhuǎn)換
這篇文章主要介紹了Python 稀疏矩陣-sparse 存儲(chǔ)和轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2017-05-05Anaconda3中的Jupyter notebook添加目錄插件的實(shí)現(xiàn)
這篇文章主要介紹了Anaconda3中的Jupyter notebook添加目錄插件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Python實(shí)現(xiàn)多線程抓取網(wǎng)頁(yè)功能實(shí)例詳解
這篇文章主要介紹了Python實(shí)現(xiàn)多線程抓取網(wǎng)頁(yè)功能,結(jié)合具體實(shí)例形式詳細(xì)分析了Python多線程編程的相關(guān)操作技巧與注意事項(xiàng),并附帶demo實(shí)例給出了多線程抓取網(wǎng)頁(yè)的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-06-06Python 如何操作 SQLite 數(shù)據(jù)庫(kù)
這篇文章主要介紹了Python 如何操作 SQLite 數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08Python標(biāo)準(zhǔn)庫(kù)pathlib操作目錄和文件
這篇文章主要為大家介紹了Python標(biāo)準(zhǔn)庫(kù)pathlib操作目錄和文件的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11使用pandas將numpy中的數(shù)組數(shù)據(jù)保存到csv文件的方法
今天小編就為大家分享一篇使用pandas將numpy中的數(shù)組數(shù)據(jù)保存到csv文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06python des,aes,rsa加解密的實(shí)現(xiàn)
這篇文章主要介紹了python des,aes,rsa加解密的實(shí)現(xiàn),幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01Python發(fā)送http請(qǐng)求解析返回json的實(shí)例
下面小編就為大家分享一篇Python發(fā)送http請(qǐng)求解析返回json的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03