python如何将mat文件转为png
作者:马鹏森 时间:2023-04-14 07:37:21
将mat文件转为png
花费了很大力气做这件事,总是出现各种错误,现在终于解决了
from PIL import Image
import matplotlib.pyplot as plt
import glob
import os
import numpy as np
import mat73
# 数据矩阵转图片的函数
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: # 判断是否存在文件夹如果不存在则创建为文件夹
os.makedirs(path) # makedirs 创建文件时如果路径不存在会创建这个路径
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--数据集/去噪/dnd_2017/input/*.mat")
save_path = "blur13x13/"
mkdir(save_path) # 调用函数
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文件夹路径即可~
将图片转换为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})
来源:https://blog.csdn.net/weixin_43135178/article/details/123471749
标签:python,mat文件,png
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现Dijkstra算法的最短路径问题
2023-05-18 23:22:15
![](https://img.aspxhome.com/file/2023/4/95784_0s.png)
详解Python Matplotlib解决绘图X轴值不按数组排序问题
2022-12-03 09:33:06
![](https://img.aspxhome.com/file/2023/0/110050_0s.png)
Python获取时光网电影数据的实例代码
2023-10-22 21:40:52
![](https://img.aspxhome.com/file/2023/9/112579_0s.png)
js function定义函数的几种不错方法
2024-04-16 09:06:34
在cmd命令行里进入和退出Python程序的方法
2023-07-18 04:21:14
![](https://img.aspxhome.com/file/2023/0/60720_0s.jpg)
javascript操作ASP.NET服务器控件
2023-07-21 15:09:26
JavaScript+Canvas实现带跳动效果的粒子动画
2024-04-23 09:08:06
![](https://img.aspxhome.com/file/2023/2/135932_0s.png)
python开启摄像头以及深度学习实现目标检测方法
2023-10-27 03:23:18
pytorch载入预训练模型后,实现训练指定层
2022-01-22 12:40:35
对Python中plt的画图函数详解
2023-08-16 08:49:07
详解将Python程序(.py)转换为Windows可执行文件(.exe)
2022-05-29 20:46:25
![](https://img.aspxhome.com/file/2023/8/105868_0s.png)
教你如何6秒钟往MySQL插入100万条数据的实现
2024-01-19 02:17:35
![](https://img.aspxhome.com/file/2023/7/105667_0s.jpg)
keras 指定程序在某块卡上训练实例
2023-07-15 09:15:42
![](https://img.aspxhome.com/file/2023/1/99041_0s.jpg)
Python NumPy随机抽模块介绍及方法
2021-04-21 16:47:03
mysql性能优化之索引优化
2024-01-15 13:51:44
![](https://img.aspxhome.com/file/2023/8/110298_0s.png)
python软件都是免费的吗
2022-09-04 03:45:53
GO语言常用的文件读取方式
2024-05-21 10:21:30
OpenCV全景图像拼接的实现示例
2021-12-04 00:36:09
![](https://img.aspxhome.com/file/2023/4/121914_0s.jpg)
使用Python对Dicom文件进行读取与写入的实现
2022-05-30 12:18:27
![](https://img.aspxhome.com/file/2023/4/135304_0s.jpg)
Centos7下mysql 8.0.15 安装配置图文教程
2024-01-20 03:15:56
![](https://img.aspxhome.com/file/2023/0/66610_0s.png)