Python 有没有更快的方法将2d numpy数组保存到png

Python 有没有更快的方法将2d numpy数组保存到png,python,numpy,png,Python,Numpy,Png,嗨,我是python新手,我正在尝试将2d numpy数组保存到png文件中 2d numpy数组中的每个元素都是0到100之间的整数,我有一个getColor()函数将其映射为rgb值。我现在所做的是构造一个3通道numpy数组,其形状与我的2d numpy数组相同,并将每个值映射到相应的rgb值。然而,这需要很多时间,我觉得应该有一个更有效的方法来做到这一点。我的代码目前处理一个图像大约需要5秒钟 将numpy导入为np 导入图像 flt_m=get2dArray()#返回一个(880*88

嗨,我是python新手,我正在尝试将2d numpy数组保存到png文件中

2d numpy数组中的每个元素都是0到100之间的整数,我有一个
getColor()
函数将其映射为rgb值。我现在所做的是构造一个3通道numpy数组,其形状与我的2d numpy数组相同,并将每个值映射到相应的rgb值。然而,这需要很多时间,我觉得应该有一个更有效的方法来做到这一点。我的代码目前处理一个图像大约需要5秒钟

将numpy导入为np
导入图像
flt_m=get2dArray()#返回一个(880*880)numpy数组
def getColor(值):
如果(值<0):
返回(0,0,0)
elif(值<50):
返回(100150200)
其他:
报税表(255255)
canvas=np.zero((flt_m.shape[0],flt_m.shape[1],3)).astype(np.uint8)
对于范围内的行(flt_m.shape[0]):
对于范围内的列(flt_m.形状[1]):
rgb=getColor(flt_m[行,列])
对于范围(3)中的i:
画布[行、列、i]=rgb[i]
imageio.imwrite('test.png',canvas)#将文件保存到png
您可以使用设置数组的不同子集

因此,也许您使用:

canvas = np.ones([880, 880, 3], dtype=np.uint8) * 255   # initialize the whole RGB-Array with (255, 255, 255)

canvas[flt_m<50] = (100, 150, 200)      # set all values where flt_m is <50 to (100, 150, 200)

对于@SpghttCd的答案,您已经有了一个很好的解决方案,但是您的写入时间似乎很慢,所以我考虑了一个替代解决方案

由于您的图像中只有2-3种颜色,您可以编写一个调色板图像(最多支持256种颜色),这将占用更少的内存、更少的处理和更少的磁盘空间。它不是为每个像素存储3个字节(1个表示红色,1个表示绿色,1个表示蓝色),而是在每个像素存储一个字节,该字节是256色RGB查找表或调色板的索引

import numpy as np
from PIL import Image

# Generate synthetic image of same size with random numbers under 256
flt_im = np.random.randint(0,256,(880,880), dtype=np.uint8)

# Make numpy array into image without allocating any more memory
p = Image.fromarray(flt_im, mode='L')

# Create a palette with 256 colours - first 50 are your blueish colour, rest are white
palette = 50*[100,150,200] +  206*[255,255,255]

# Put palette into image and save
p.putpalette(palette)
p.save('result.png')
显然,我无法检查您的机器的性能,但如果我将我的palettised版本与SpightTCD的版本进行比较,我会得到50倍的速度差:

def SpghttCd(flt_im):
    canvas = np.ones([880, 880, 3], dtype=np.uint8) * 255

    canvas[flt_im<50] = (100, 150, 200)
    imageio.imwrite('SpghttCd.png', canvas)


def me(flt_im):
    # Make numpy array into image without allocating any more memory
    p = Image.fromarray(flt_im, mode='L')

    # Create a palette with 256 colours - first 50 are your blueish colour, rest are white
    palette = 50*[100,150,200] +  206*[255,255,255]

    # Put palette into image and save
    p.putpalette(palette)
    p.save('result.png')

# Generate random data to test with - same for both
flt_im = np.random.randint(0,256,(880,880), dtype=np.uint8)

%timeit me(flt_im)
In [34]: %timeit me(flt_im)                                                                         
34.1 ms ± 1.06 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [37]: %timeit SpghttCd(flt_im)                                                                   
1.68 s ± 7.17 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
def SpghttCd(flt_im):
canvas=np.one([880880,3],dtype=np.uint8)*255

canvas[flt\u im你的问题说你的值在0到100之间,但是你的代码测试的是负值吗?你试过
canvas[row,col]=rgb
?模块
枕头
也可以将numpy.array转换为图像-也许它会工作得更快。你可能应该在
np.ones()上设置
dtype=np.uint8
为了避免生成float64 type.setting
dtype=np.uint8
真的很有帮助,现在执行时间减少了2秒。@Steven97102对于880x880来说似乎非常慢。你是在使用树莓Pi还是类似的东西?嗯,我实际上是在英特尔i5上运行的,不知道为什么这么慢这太神奇了!
def SpghttCd(flt_im):
    canvas = np.ones([880, 880, 3], dtype=np.uint8) * 255

    canvas[flt_im<50] = (100, 150, 200)
    imageio.imwrite('SpghttCd.png', canvas)


def me(flt_im):
    # Make numpy array into image without allocating any more memory
    p = Image.fromarray(flt_im, mode='L')

    # Create a palette with 256 colours - first 50 are your blueish colour, rest are white
    palette = 50*[100,150,200] +  206*[255,255,255]

    # Put palette into image and save
    p.putpalette(palette)
    p.save('result.png')

# Generate random data to test with - same for both
flt_im = np.random.randint(0,256,(880,880), dtype=np.uint8)

%timeit me(flt_im)
In [34]: %timeit me(flt_im)                                                                         
34.1 ms ± 1.06 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [37]: %timeit SpghttCd(flt_im)                                                                   
1.68 s ± 7.17 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)