繁体   English   中英

如何plot 一个灰度图像并保存它?

[英]How to plot a gray image and save it?

我正在使用 matplotlib 到 plot function 图,比如正弦 ZC1C425Z5268E687A94D1

t = np.arange(0., 5., 0.2)
plt.plot(t, np.sin(t))

但它有 3 个频道。 我应该如何制作灰度图像(只有一个通道)并将其保存为形状高度 * 宽度 * 1 的 numpy 数组。谢谢!

使用skimage库可能会有管道(它仍然是相当重量级的,但现在图像的质量会好得多)。

#import the required libraries and functions
import os
from skimage.io import imread
from skimage.color import rgb2gray, rgba2rgb

#create an image
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
plt.plot(x, y)

#save and image as a .png picture and then load 
plt.savefig('img.png')
img = imread('img.png')
#we do not need the colored picture, so remove it
os.remove('img.png')

#the loaded picture is initially rgba, convert it to the grayscale image
img = rgb2gray(rgba2rgb(img))
#if you want a flat array saved as binary, ravel() it and use np.save
np.save('test.npy', img.ravel())

暂无
暂无

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

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