简体   繁体   English

显示一个二维 numpy 数组,其中内容是元组作为图像

[英]Show a 2d numpy array where contents are tuples as an image

I have a 2d (M * N) numpy array, where each cell contains a tuple of length 3. The tuple contains R, G, and B values for each pixel, eg我有一个 2d (M * N) numpy 数组,其中每个单元格包含一个长度为 3 的元组。该元组包含每个像素的 R、G 和 B 值,例如

A[0,0] = (0, 255, 0)

Is there a way to show this array as an image, possibly without the need to transform it first to a 3d (M * N *3) array?有没有办法将此数组显示为图像,可能不需要先将其转换为 3d (M * N *3) 数组?

Image from PIL should be able to generate a picture from a M*N matrix with RGB as a tuple.来自 PIL 的图像应该能够从以 RGB 作为元组的 M*N 矩阵生成图片。

from PIL import Image
import numpy as np
matrix = np.array([[(100,100,100),(100,100,100),(100,100,100)],[(100,100,100),(100,255,100),(100,100,100)],[(100,100,100),(100,100,100),(100,100,100)]])
im = Image.fromarray(matrix, 'RGB')
im.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM