从 MatLab 数据在 Python 中创建 .PNG 图像 [英] Create .PNG images in Python from MatLab data
问题描述
实际上我正在尝试在 Python 中创建 .png 图像.数据来自 Matlab 文件.这是我的代码.当我运行时出现错误:
Actually i'm trying to create .png images in Python. The data is get from a Matlab file. Here my code. When i running i have the error:
回溯(最近一次调用最后一次):文件readFromMatLab.py",第 20 行,在矩阵[x][y][z] = 数据[x][y][z]索引错误:列表索引超出范围
Traceback (most recent call last): File "readFromMatLab.py", line 20, in matriz[x][y][z] = data[x][y][z] IndexError: list index out of range
Matlab 文件的数据是 512x512x200 双数组.
The data of the Matlab file is 512x512x200 Double array.
> {'__version__': '1.0', 'St3D': array([[[ -4.98510788e-02,
> -4.98139346e-02, -4.97636073e-02, ...,
> -5.19862428e-02, -5.20095813e-02, -5.20122990e-02],
> [ -4.98249255e-02, -4.97792210e-02, -4.97507640e-02, ...,
> -5.19832396e-02, -5.19884452e-02, -5.20089354e-02],
> [ -4.98121755e-02, -4.97751679e-02, -4.97488529e-02, ...,
> -5.19605824e-02, -5.19734534e-02, -5.20023879e-02],
> ...,
> [[ 9.10799464e-05, 1.75287655e-04, 2.26928715e-04, ...,
> 1.10619951e-04, 1.04038395e-04, 7.44506576e-05],
> [ 6.29097917e-05, 1.20765020e-04, 1.91577341e-04, ...,
> 8.24078623e-05, 8.96774520e-05, 7.44268856e-05],
> [ 4.14273859e-05, 7.96562916e-05, 1.20801256e-04, ...,
> 9.05750282e-05, 8.13201896e-05, 6.77554603e-05],
> ...,
> [ 1.72297366e-04, 1.68849830e-04, 2.21771692e-04, ...,
> 2.30046391e-04, 2.51247428e-04, 2.58021432e-04],
> [ 2.06350049e-04, 1.92126121e-04, 2.58923928e-04, ...,
> 2.48977658e-04, 2.78131275e-04, 2.76242136e-04],
> [ 2.42915268e-04, 2.47607632e-04, 2.89283796e-04, ...,
> 2.58819021e-04, 2.76203977e-04, 2.82977241e-04]]]), '__header__': 'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Fri
> Sep 19 17:03:17 2014', '__globals__': []}
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pprint
import scipy.io
import numpy
from scipy import misc
from PIL import Image
# import file into a dictionary
fMatLab = scipy.io.loadmat('St3D', mat_dtype = True, squeeze_me = True, struct_as_record=False)
# read in the structure
data = fMatLab['St3D']
matriz = [[[0 for col in range(data.shape[0])] for row in range(data.shape[1])] for x in range(data.shape[2])]
for x in range(0,data.shape[0]):
for y in range(0,data.shape[1]):
for z in range(0,data.shape[2]):
matriz[x][y][z] = data[x][y][z]
for i in range(len(matriz)):
#im = numpy.random.random_integers(0, 255, 512*512).reshape((512, 512))
misc.imsave('transect_%s.png' % i, matriz[i])
from glob import glob
filelist = glob('transect*.png')
filelist.sort()
推荐答案
在我看来,您正在尝试将 misc.imsave
应用到数据的特定维度上的每个切片.但是代码效率很低,尤其是从matriz =
到matriz[x][y][z] =
.您已经将矩阵作为 numpy 数组存储在变量 data
中,因此不需要列表理解或循环.如果你想在一维上循环 numpy 数组的切片,例如,第三维,以应用一个函数,只需执行以下操作:
It looks to me like you are trying to apply misc.imsave
to each slice along a certain dimension of your data. However, the code is very inefficient, especially from matriz =
to matriz[x][y][z] =
. You already have the matrix stored as a numpy array in the variable data
, so there is no need for the list comprehension or the loops. If you want to loop over slices of a numpy array across one dimension, say, for example, the third dimension, to apply a function, just do something like this:
for i in xrange(data.shape[2]):
some_function(data[:, :, i])
编辑在你的情况下,这将转化为
EDIT In your case, this would translate to
data = fMatLab['St3D']
for i in xrange(data.shape[2]):
misc.imsave('transect_%s.png' % i, data[:, :, i])
此外,最后三行不应缩进.
And, as a quick final note, the last three lines shouldn't be indented.
这篇关于从 MatLab 数据在 Python 中创建 .PNG 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!