简体   繁体   English

使用 matplotlib.image.imsave 和 matplotlib.pyplot.savefig 保存一个 tiff 光栅文件给出不同大小的输出

[英]Saving a tiff raster file with matplotlib.image.imsave and matplotlib.pyplot.savefig give outputs of different sizes

I have a numpy array of dimension (10980x10980).我有一个维度为 numpy 的数组 (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.当我使用matplotlib.image.imsave('file.tiff',data)保存它时, viridis pseudo colorscheme 自动应用于 plot。我这样做了,并获得了一个 480 MB 的 tiff 文件。

If I save the same figure using matplotlib.pyplot ,如果我使用matplotlib.pyplot保存相同的图形,

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

I obtain a file of around 1.5 MB.我获得了一个大约 1.5 MB 的文件。

The tiff info of both the files are:这两个文件的 tiff 信息是:

  1. Using matplotlib.image.imsave :使用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 :使用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.我们可以看到 plt.savefig 已经将尺寸缩小到 640x480。 Why was it?为什么会这样? And how to change this?以及如何改变这一点?

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.根据pyplot.savefig 的文档,如果未设置dpi参数,它将默认使用图形的 dpi 值,这会影响分辨率。

The documentation for image.imsave states that it does not affect the resolution. image.imsave的文档指出它不会影响分辨率。

Edit addressing the author's comment on my answer: DPI is simply pixels per inch.编辑解决作者对我的回答的评论:DPI 就是每英寸像素数。 image size = |px|/dpi = 1098/10 = 109.8 inches.图像大小 = |px|/dpi = 1098/10 = 109.8 英寸。 10980/1 = 10980 inches, which makes the image 100x bigger. 10980/1 = 10980 英寸,使图像放大 100 倍。

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. matplotlib.image.imsave('file.tiff',data)将'data'中包含的数据保存成一个tiff(本质上是一个数组,可以看作是一个图像。

plt.imshow(data); plt.colorbar(); plt.savefig('file.tiff') 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. plt.imshow(data); plt.colorbar(); plt.savefig('file.tiff')正在创建一个 matplotlib 图,显示数据中存储的数据,然后使用默认参数(dpi 等)将保存为 tiff。 Both syntaxes are correct, it depends on what your use case is.两种语法都是正确的,这取决于您的用例。

暂无
暂无

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

相关问题 为什么 matplotlib.pyplot.savefig() 会弄乱非常大的 pandas.plotting.scatter_matrix() 的图像输出? - Why does matplotlib.pyplot.savefig() mess up image outputs for very large pandas.plotting.scatter_matrix()? Matplotlib (pyplot) savefig 输出空白图像 - Matplotlib (pyplot) savefig outputs blank image 在 python 中使用 matplotlib.pyplot.savefig() 的更快方法 - Faster way to use matplotlib.pyplot.savefig() in python 在matplotlib.pyplot.savefig()filename参数中使用字符串格式获取ValueError - Getting ValueError with string formatting in matplotlib.pyplot.savefig() filename argument matplotlib.image.imsave TypeError:无法将图像数据转换为float - matplotlib.image.imsave TypeError: Image data cannot be converted to float 为什么 matplotlib.pyplot.imsave() 和 matplotlib.pyplot.imshow() 图像质量不同? 如何保证同样的质量? - Why matplotlib.pyplot.imsave() and matplotlib.pyplot.imshow() image qualities are different? How to ensure the same quality? Python:如何使用 matplotlib.image.imsave() 将 EXACT numpy 数组数据保存到图像 - Python: How to save EXACT numpy array data to image using matplotlib.image.imsave() 在子进程中运行 matplotlib.pyplot.savefig 时,什么会导致死锁或冻结? - When running matplotlib.pyplot.savefig in a subprocess, what can cause a deadlock or a freeze? 使用matplotlib.pyplot.imsave将numpy.ndarray保存为图像(.png)时出错 - Error saving a numpy.ndarray as an image (.png) using matplotlib.pyplot.imsave Matplotlib使用日志规范保存图像 - Matplotlib imsave image with log norm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM