欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python如何將mat文件轉(zhuǎn)為png

 更新時(shí)間:2022年07月15日 11:06:22   作者:馬鵬森  
這篇文章主要介紹了python如何將mat文件轉(zhuǎn)為png,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

將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)文章

最新評(píng)論