简体   繁体   English

将 numpy 数组保存为图像的问题

[英]Problems with saving numpy array as an image

I have troubles with saving numpy two-dimensional array as image (in grayscale).我无法将 numpy 二维数组保存为图像(灰度)。 I read this question Save plot to image file instead of displaying it using Matplotlib , but still don't understand what's wrong with the following code.我读了这个问题Save plot to image file 而不是使用 Matplotlib 显示它,但仍然不明白以下代码有什么问题。

plt.imsave("image.eps", np.flipud(arr), cmap = "gray_r", format = "eps", dpi = 1000)

After execution the image.eps is blank and I don't understand why.执行后 image.eps 是空白的,我不明白为什么。 I also tried to deal with it as an AxesImage object (see below) but without success.我还尝试将其作为 AxesImage object (见下文)处理,但没有成功。

img = plt.imshow(np.flipud(arr))
plt.savefig("image.eps", format = "eps", dpi = 1000)

Please, explain me what's wrong with this code.请解释一下这段代码有什么问题。

Actually, I am new to Python (and to matplotlib as well), so I am not familiar with many ideas of Python.实际上,我是 Python(以及 matplotlib)的新手,所以我对 Python 的许多想法并不熟悉。

Update: I tried the following code but the image.eps is still blank.更新:我尝试了以下代码,但 image.eps 仍然是空白的。

import matplotlib.pyplot as plt
import numpy as np

arr = np.array([[1, 0.5],[0.5, 0]])

plt.imsave("image.eps", np.flipud(arr), cmap = "gray_r", format = "eps", dpi = 1000)

On the other hand if I call另一方面,如果我打电话

img = plt.imshow(arr)

then I can see in the console (Spyder) the correct picture (one black, one white and two gray squares).然后我可以在控制台(Spyder)中看到正确的图片(一个黑色,一个白色和两个灰色方块)。

Maybe there is some way to save the picture using the img-variable?也许有一些方法可以使用 img 变量保存图片?

I have tested imsave in my interpreter我在我的解释器中测试imsave

In [39]: import numpy as np 
    ...: import matplotlib.pyplot as plt                                                  

In [40]: a = np.random.random((500,500))                                                  

In [41]: plt.imsave('a1000.eps', a, dpi=1000)                                             

In [42]: plt.imsave('a0500.eps', a, dpi=500)                                              

In [43]:                                                                                  
Do you really want to exit ([y]/n)? 
22:07 boffi@debian:~ $ ll a1000.eps a0500.eps 
-rw-r--r-- 1 boffi boffi 1512480 Nov 22 22:07 a0500.eps
-rw-r--r-- 1 boffi boffi 1512480 Nov 22 22:07 a1000.eps

As you can see, the two files have identical sizes, are they equal or are they different?如您所见,这两个文件具有相同的大小,它们是相同的还是不同的?

22:08 boffi@debian:~ $ diff a1000.eps a0500.eps 
2c2
< %%Title: a1000.eps
---
> %%Title: a0500.eps
4c4
< %%CreationDate: Fri Nov 22 22:07:30 2019
---
> %%CreationDate: Fri Nov 22 22:07:42 2019
6c6
< %%BoundingBox: 288.0 378.0 324.0 414.0
---
> %%BoundingBox: 270.0 360.0 342.0 432.0
31,32c31,32
< 288 378 translate
< 36 36 0 0 clipbox
---
> 270 360 translate
> 72 72 0 0 clipbox
37c37
< 36.0 36.0 scale
---
> 72.0 72.0 scale

As you can see, the differences are only in the postscript code, especially the scale command has interestingly different parameters... on the contrary we see that the raw data representing the matrix is exactly the same in the two files.如您所见,差异仅在后记代码中,尤其是scale命令具有有趣的不同参数……相反,我们看到表示矩阵的原始数据在两个文件中完全相同 This is consistent with the description of dpi in the imsave doc string这与imsave doc字符串中dpi的描述一致

dpi: int The DPI to store in the metadata of the file. This does not affect the resolution of the output image.

Finally, we have the problem that your images are blank.最后,我们的问题是您的图像是空白的。 Mine are not.我的不是。 Note that the larger image is the one with lesser dpi , this is contrary to your belief that increasing the dpi you'll get a larger picture.请注意,较大的图像是具有较小dpi的图像,这与您认为增加dpi会获得更大图像的信念相反。

在此处输入图像描述 在此处输入图像描述

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

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