[英]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 信息是:
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
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.