简体   繁体   English

如何使用 matplotlib/numpy 将数组保存为灰度图像?

[英]how to save an array as a grayscale image with matplotlib/numpy?

I am trying to save a numpy array of dimensions 128x128 pixels into a grayscale image.我正在尝试将尺寸为 128x128 像素的 numpy 数组保存到灰度图像中。 I simply thought that the pyplot.imsave function would do the job but it's not, it somehow converts my array into an RGB image.我只是认为 pyplot.imsave 函数可以完成这项工作,但事实并非如此,它以某种方式将我的数组转换为 RGB 图像。 I tried to force the colormap to Gray during conversion but eventhough the saved image appears in grayscale, it still has a 128x128x4 dimension.我试图在转换过程中将颜色图强制为灰色,但即使保存的图像以灰度显示,它仍然具有 128x128x4 尺寸。 Here is a code sample I wrote to show the behaviour :这是我编写的用于显示行为的代码示例:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mplimg
from matplotlib import cm

x_tot = 10e-3
nx = 128

x = np.arange(-x_tot/2, x_tot/2, x_tot/nx)

[X, Y] = np.meshgrid(x,x)
R = np.sqrt(X**2 + Y**2)

diam = 5e-3
I = np.exp(-2*(2*R/diam)**4)

plt.figure()
plt.imshow(I, extent = [-x_tot/2, x_tot/2, -x_tot/2, x_tot/2])

print I.shape

plt.imsave('image.png', I)
I2 = plt.imread('image.png')
print I2.shape

mplimg.imsave('image2.png',np.uint8(I), cmap = cm.gray)
testImg = plt.imread('image2.png')
print testImg.shape

In both cases the results of the "print" function are (128,128,4).在这两种情况下,“打印”函数的结果都是 (128,128,4)。

Can anyone explain why the imsave function is creating those dimensions eventhough my input array is of a luminance type?谁能解释为什么 imsave 函数会创建这些维度,即使我的输入数组是亮度类型? And of course, does anyone have a solution to save the array into a standard grayscale format?当然,有没有人有将数组保存为标准灰度格式的解决方案?

Thanks!谢谢!

With PIL it should work like this使用PIL它应该像这样工作

import Image

I8 = (((I - I.min()) / (I.max() - I.min())) * 255.9).astype(np.uint8)

img = Image.fromarray(I8)
img.save("file.png")

There is also an alternative of using imageio .还有一种使用imageio的替代方法。 It provides an easy and convenient API and it is bundled with Anaconda.它提供了一个简单方便的 API,并与 Anaconda 捆绑在一起。 It can save grayscale images as a single color channel file.它可以将灰度图像保存为单色通道文件。

Quoting the documentation引用文档

>>> import imageio
>>> im = imageio.imread('imageio:astronaut.png')
>>> im.shape  # im is a numpy array
(512, 512, 3)
>>> imageio.imwrite('astronaut-gray.jpg', im[:, :, 0])

I didn't want to use PIL in my code and as noted in the question I ran into the same problem with pyplot, where even in grayscale, the file is saved in MxNx3 matrix.我不想在我的代码中使用 PIL,并且如问题中所述,我遇到了与 pyplot 相同的问题,即使在灰度中,文件也保存在 MxNx3 矩阵中。

Since the actual image on disk wasn't important to me, I ended up writing the matrix as is and reading it back "as-is" using numpy's save and load methods:由于磁盘上的实际图像对我来说并不重要,我最终按原样编写矩阵并使用 numpy 的保存和加载方法“原样”读取它:

np.save("filename", image_matrix)

And:和:

np.load("filename.npy")

There is also a possibility to use scikit-image , then there is no need to convert numpy array into a PIL object.也有可能使用scikit-image ,那么就不需要将 numpy 数组转换为 PIL 对象。

from skimage import io
io.imsave('output.tiff', I.astype(np.uint16))

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

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