Home > Software design >  Saving a tiff raster file with matplotlib.image.imsave and matplotlib.pyplot.savefig give outputs of
Saving a tiff raster file with matplotlib.image.imsave and matplotlib.pyplot.savefig give outputs of

Time:09-29

I have a numpy array of dimension (10980x10980).

When I save it using matplotlib.image.imsave('file.tiff',data), viridis pseudo colorscheme is automatically applied to the plot. I did this, and obtained a tiff file of 480 MB.

If I save the same figure using matplotlib.pyplot,

plt.imshow(data)
plt.colorbar()
plt.savefig('file.tiff')

I obtain a file of around 1.5 MB.

The tiff info of both the files are:

  1. Using matplotlib.image.imsave:
TIFF Directory at offset 0x8 (8)
  Image Width: 10980 Image Length: 10980
  Resolution: 100, 100 pixels/inch
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Extra Samples: 1<unassoc-alpha>
  Samples/Pixel: 4
  Rows/Strip: 10980
  Planar Configuration: single image plane
  1. Using pyplot.savefig:
TIFF Directory at offset 0x8 (8)
  Image Width: 640 Image Length: 480
  Resolution: 100, 100 pixels/inch
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Extra Samples: 1<unassoc-alpha>
  Samples/Pixel: 4
  Rows/Strip: 480
  Planar Configuration: single image plane

We can see that plt.savefig has reduced the dimensions to 640x480. Why was it? And how to change this?

CodePudding user response:

As per pyplot.savefig's documentation, if the dpi argument is not set, it will default to using the figure's dpi value which can affect the resolution.

The documentation for image.imsave states that it does not affect the resolution.

CodePudding user response:

You are doing two different operations.

matplotlib.image.imsave('file.tiff',data) saves the data contained in 'data' into a tiff (essentially an array that can be viewed as an image.

plt.imshow(data); plt.colorbar(); plt.savefig('file.tiff') is creating a matplotlib figure, showing the data stored in data and then using the default parameters (dpi etc.) to save the figure as a tiff. Both syntaxes are correct, it depends on what your use case is.

  • Related