0

I have troubles with saving numpy two-dimensional array as image (in grayscale). 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.

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. I also tried to deal with it as an AxesImage object (see below) but without success.

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.

Update: I tried the following code but the image.eps is still blank.

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).

Maybe there is some way to save the picture using the img-variable?

richrow
  • 105
  • 4
  • 2
    Can you include the array that you are using? Or at least a subset of it? – William Miller Nov 21 '19 at 20:56
  • 2
    With `arr = np.arange(10000).reshape(100,100)`, your `imsave` syntax works (though the image is only 1 inch wide) . So you need to be more specific. – Demi-Lune Nov 21 '19 at 21:16
  • 1
    I suspect one major problem is that eps, like other vector formats, does not really have a dpi defined. This may have some strange implications if the canonical "dpi" of 72 is completely different from the set dpi (here 1000). – ImportanceOfBeingErnest Nov 22 '19 at 00:07
  • Yes, indeed, code without setting dpi works. However, the corresponding picture is too small (even for matrices 500\times 500 that I use). Is it possible to make picture bigger? – richrow Nov 22 '19 at 19:38

1 Answers1

1

I have tested imsavein my interpreter

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. This is consistent with the description of dpi in the imsave doc string

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.

enter image description hereenter image description here

gboffi
  • 22,939
  • 8
  • 54
  • 85