matlab 创建彩色图像,用matlabd在Python中创建彩色图像

我正在尝试用Python创建彩色图像。数据来自Matlab文件。

这是我的密码。实际上我只能创建灰度图像,但我需要彩色图像。

你能帮帮我吗?在

Matlab文件的数据是512x512x200双数组。在> {'__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 scipy.io

from scipy import misc

from PIL import Image

##import Image

import numpy as np

import timeit

fDataName = 'MatLab_File'

start_time = timeit.default_timer()

# import file into a dictionary

fMatLab = scipy.io.loadmat(fDataName, mat_dtype = True, squeeze_me = True, struct_as_record=False)

# read in the structure

data = fMatLab[fDataName]

sizeZ = data.shape[2]

for i in range(sizeZ):

img = scipy.misc.toimage(data[:,:,i], low=-0, high=255, mode='L')

img.save('imageFinal_%s.png' % i)

stop_time = timeit.default_timer() - start_time

print("--- %s seconds ---" % stop_time)

使用该代码创建映像的示例如下:

O0KpI.png

所需的彩色图像如下:

P5UmT.png

彩色图像是用以下代码创建的:

^{pr2}$

我还使用了这段代码:for i in range(sizeZ):

img = scipy.misc.toimage(data[:,:,i], low=0, high=255, mode='L')

img.save('transectxyi_%s.png' % i)

im = Image.fromarray(data[:,:,i]).convert('RGBA')

im.save("a.png")

但我有黑色的图像。黑色图像如下:

0SPg3.png

编辑:我用代码解决了我的问题:def saveImages(data, fileName):

img = scipy.misc.toimage(data, low=0, high=255, mode='P')

imP = img.convert('RGBA')

img = mpimg.pil_to_array(imP)

imgP = img[:,:,0]

plt.imsave(fileName, imgP, vmin=None, vmax=None, cmap=None, format=None, origin=None, dpi=100)

return imP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值