如何将 numpy 数组转换为(并显示)图像? [英] How do I convert a numpy array to (and display) an image?
本文介绍了如何将 numpy 数组转换为(并显示)图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这样创建了一个数组:
I have created an array thusly:
import numpy as np
data = np.zeros( (512,512,3), dtype=np.uint8)
data[256,256] = [255,0,0]
我希望这样做是在 512x512 图像的中心显示一个红点.(至少开始时......我想我可以从那里弄清楚其余的)
What I want this to do is display a single red dot in the center of a 512x512 image. (At least to begin with... I think I can figure out the rest from there)
推荐答案
您可以使用 PIL 创建(和显示)图像:
You could use PIL to create (and display) an image:
from PIL import Image
import numpy as np
w, h = 512, 512
data = np.zeros((h, w, 3), dtype=np.uint8)
data[0:256, 0:256] = [255, 0, 0] # red patch in upper left
img = Image.fromarray(data, 'RGB')
img.save('my.png')
img.show()
这篇关于如何将 numpy 数组转换为(并显示)图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文