在 Python 中将3个独立的 numpy 数组组合成一个 RGB 图像

所以我有一组数据,我可以把它们转换成 R,G,B 波段的数组。现在我需要组合它们来形成一个 RGB 图像。

我尝试“图像”来做这项工作,但它需要“模式”被归属。

我想变个魔术。我将使用 Image.frmarray ()将数组转换为 image,但是默认情况下,当 Image.merge 需要合并‘ L’模式的图像时,数组将达到‘ F’模式。如果我首先声明 frmarray ()中的 array 属性为‘ L’,那么所有的 R G B 图像都会失真。

但是,如果我保存图像,然后打开它们,然后合并,它工作得很好。图像以“ L”模式读取图像。

现在我有两个问题。

首先,我不认为这是一种优雅的工作方式。所以如果有人知道更好的方法,请告诉我

其次,Image.Save 不能正常工作。我面临的错误如下:

In [7]: Image.SAVE(imagefile, 'JPEG')
----------------------------------------------------------------------------------


TypeError                                 Traceback (most recent call last)


/media/New Volume/Documents/My own works/ISAC/SAMPLES/<ipython console> in <module>()


TypeError: 'dict' object is not callable

请提出解决方案。

请注意,图像大约是4000x4000大小的数组。

125039 次浏览

I don't really understand your question but here is an example of something similar I've done recently that seems like it might help:

# r, g, and b are 512x512 float arrays with values >= 0 and < 1.
from PIL import Image
import numpy as np
rgbArray = np.zeros((512,512,3), 'uint8')
rgbArray[..., 0] = r*256
rgbArray[..., 1] = g*256
rgbArray[..., 2] = b*256
img = Image.fromarray(rgbArray)
img.save('myimg.jpeg')

I hope that helps

Convert the numpy arrays to uint8 before passing them to Image.fromarray

Eg. if you have floats in the range [0..1]:

r = Image.fromarray(numpy.uint8(r_array*255.999))
rgb = np.dstack((r,g,b))  # stacks 3 h x w arrays -> h x w x 3

To also convert floats 0 .. 1 to uint8 s,

rgb_uint8 = (np.dstack((r,g,b)) * 255.999) .astype(np.uint8)  # right, Janna, not 256

Your distortion i believe is caused by the way you are splitting your original image into its individual bands and then resizing it again before putting it into merge;

`
image=Image.open("your image")


print(image.size) #size is inverted i.e columns first rows second eg: 500,250


#convert to array
li_r=list(image.getdata(band=0))
arr_r=np.array(li_r,dtype="uint8")
li_g=list(image.getdata(band=1))
arr_g=np.array(li_g,dtype="uint8")
li_b=list(image.getdata(band=2))
arr_b=np.array(li_b,dtype="uint8")


# reshape
reshaper=arr_r.reshape(250,500) #size flipped so it reshapes correctly
reshapeb=arr_b.reshape(250,500)
reshapeg=arr_g.reshape(250,500)


imr=Image.fromarray(reshaper,mode=None) # mode I
imb=Image.fromarray(reshapeb,mode=None)
img=Image.fromarray(reshapeg,mode=None)


#merge
merged=Image.merge("RGB",(imr,img,imb))
merged.show()
`

this works well !

rgb = np.dstack((r,g,b))  # stacks 3 h x w arrays -> h x w x 3

This code doesnt create 3d array if you pass 3 channels. 2 channels remain.

If using PIL Image convert it to array and then proceed with the below, else using matplotlib or cv2 perform directly.

image = cv2.imread('')[:,:,::-1]
image_2 = image[10:150,10:100]
print(image_2.shape)


img_r = image_2[:,:,0]
img_g = image_2[:,:,1]
img_b = image_2[:,:,2]


image_2 = img_r*0.2989 + 0.587*img_g + 0.114*img_b


image[10:150,10:100,0] = image_2
image[10:150,10:100,1] = image_2
image[10:150,10:100,2] = image_2


plt.imshow(image,cmap='gray')