本文介绍了将2d数组转换为python中的彩色图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2d个这样的整数列表:

I have 2d list of ints like this:

list1 = [[1, 30, 50], [21, 45, 9], [97, 321, 100]]

接下来,我将其转换为numpy数组:

Next i am going to convert this to numpy array:

myarr = np.asarray(list1)

接下来,我将使用PIL将其转换为图像,如下所示:

Next i am going to convert this to Image using PIL like this:

img = Image.fromarray(myarr, "I")
img.save("my.png")

问题是我不需要灰度图像.我不知道如何将其转换为彩色图像.我必须使用eny map函数还是其他?

the problem is that i dont want image in grayscale. I dont know how to convert this in to collored image. I have to use eny map function or somethig else ?

推荐答案

要实现此目的的方法是使用numpy

The way to accomplish this is with numpy

import numpy as np
list1 = [[1, 30, 50], [21, 45, 9], [97, 321, 100]]
list1 = np.array(list1).reshape(-1, 3)

现在list1将具有N x 3的形状,其中3维为RGB.如果您知道最终图像的大小,则可以

And now list1 will have the shape N x 3, where the 3 dimension is RGB. If you know the sizes of the final image, you can do

np.array(list1).reshape(N, M, 3)

,它将根据需要将阵列重塑为RGB.然后,有了numpy数组后,您的数组就变成了图像形状,可以将其保存为PNG等.

and it will re-shape your array into RGB as you need. Then once you have the numpy array, you have your array in the shape of an image and can save it to PNG, etc.

这篇关于将2d数组转换为python中的彩色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:38