以像素为单位指定和保存具有精确大小的图形 [英] Specifying and saving a figure with exact size in pixels

查看:33
本文介绍了以像素为单位指定和保存具有精确大小的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个大小为 3841 x 7195 像素的图像.我想将图形的内容保存到磁盘,从而生成我以像素为单位指定的确切大小的图像.

Say I have an image of size 3841 x 7195 pixels. I would like to save the contents of the figure to disk, resulting in an image of the exact size I specify in pixels.

没有轴,没有标题.只是图像.我个人并不关心 DPI,因为我只想以磁盘以像素为单位指定图像在屏幕中所占的大小.

No axis, no titles. Just the image. I don't personally care about DPIs, as I only want to specify the size the image takes in the screen in disk in pixels.

我已阅读其他 线程,它们似乎都转换为英寸,然后以英寸为单位指定图形的尺寸并调整 dpi某种方式.我想避免处理可能因像素到英寸的转换而导致的潜在精度损失.

I have read other threads, and they all seem to do conversions to inches and then specify the dimensions of the figure in inches and adjust dpi's in some way. I would like to avoid dealing with the potential loss of accuracy that could result from pixel-to-inches conversions.

我尝试过:

w = 7195
h = 3841
fig = plt.figure(frameon=False)
fig.set_size_inches(w,h)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(im_np, aspect='normal')
fig.savefig(some_path, dpi=1)

运气不好(Python 抱怨宽度和高度都必须低于 32768 (?))

with no luck (Python complains that width and height must each be below 32768 (?))

从我看到的一切来看,matplotlib 要求在inchesdpi 中指定图形大小,但我只对<强>像素图形在磁盘中的占用.我该怎么做?

From everything I have seen, matplotlib requires the figure size to be specified in inches and dpi, but I am only interested in the pixels the figure takes in disk. How can I do this?

澄清:我正在寻找一种使用 matplotlib 而不是其他图像保存库来做到这一点的方法.

To clarify: I am looking for a way to do this with matplotlib, and not with other image-saving libraries.

推荐答案

Matplotlib 不直接处理像素,而是处理物理尺寸和 DPI.如果要显示具有特定像素大小的图形,则需要知道显示器的 DPI.例如,此链接会为您检测.

Matplotlib doesn't work with pixels directly, but rather physical sizes and DPI. If you want to display a figure with a certain pixel size, you need to know the DPI of your monitor. For example this link will detect that for you.

如果你有一个 3841x7195 像素的图像,你的监视器不太可能那么大,所以你将无法显示那个大小的图形(matplotlib 要求图形适合屏幕,如果你问对于太大的尺寸,它将缩小到屏幕尺寸).假设您想要一个 800x800 像素的图像,仅作为示例.以下是在我的显示器中显示 800x800 像素图像的方法 (my_dpi=96):

If you have an image of 3841x7195 pixels it is unlikely that you monitor will be that large, so you won't be able to show a figure of that size (matplotlib requires the figure to fit in the screen, if you ask for a size too large it will shrink to the screen size). Let's imagine you want an 800x800 pixel image just for an example. Here's how to show an 800x800 pixel image in my monitor (my_dpi=96):

plt.figure(figsize=(800/my_dpi, 800/my_dpi), dpi=my_dpi)

所以你基本上只是用你的 DPI 来划分以英寸为单位的尺寸.

So you basically just divide the dimensions in inches by your DPI.

如果你想保存一个特定大小的图形,那又是另外一回事了.屏幕 DPI 不再那么重要(除非您要求的数字不适合屏幕).使用相同的800x800像素图形示例,我们可以使用savefigdpi关键字将其保存为不同的分辨率.要将其保存为与屏幕相同的分辨率,只需使用相同的 dpi:

If you want to save a figure of a specific size, then it is a different matter. Screen DPIs are not so important anymore (unless you ask for a figure that won't fit in the screen). Using the same example of the 800x800 pixel figure, we can save it in different resolutions using the dpi keyword of savefig. To save it in the same resolution as the screen just use the same dpi:

plt.savefig('my_fig.png', dpi=my_dpi)

要将其保存为 8000x8000 像素的图像,请使用 10 倍大的 dpi:

To to save it as an 8000x8000 pixel image, use a dpi 10 times larger:

plt.savefig('my_fig.png', dpi=my_dpi * 10)

请注意,并非所有后端都支持 DPI 的设置.这里使用了 PNG 后端,但 pdf 和 ps 后端将实现不同的大小.此外,更改 DPI 和大小也会影响字体大小等内容.较大的 DPI 将保持相同的字体和元素的相对大小,但如果您希望为较大的图形使用较小的字体,则需要增加物理大小而不是 DPI.

Note that the setting of the DPI is not supported by all backends. Here, the PNG backend is used, but the pdf and ps backends will implement the size differently. Also, changing the DPI and sizes will also affect things like fontsize. A larger DPI will keep the same relative sizes of fonts and elements, but if you want smaller fonts for a larger figure you need to increase the physical size instead of the DPI.

回到您的示例,如果您想保存 3841 x 7195 像素的图像,您可以执行以下操作:

Getting back to your example, if you want to save a image with 3841 x 7195 pixels, you could do the following:

plt.figure(figsize=(3.841, 7.195), dpi=100)
( your code ...)
plt.savefig('myfig.png', dpi=1000)

请注意,我使用 100 的图形 dpi 以适合大多数屏幕,但使用 dpi=1000 保存以达到所需的分辨率.在我的系统中,这会生成一个 3840x7190 像素的 png——似乎保存的 DPI 总是比所选值小 0.02 像素/英寸,这将对大图像尺寸产生(小)影响.对此的更多讨论此处.

Note that I used the figure dpi of 100 to fit in most screens, but saved with dpi=1000 to achieve the required resolution. In my system this produces a png with 3840x7190 pixels -- it seems that the DPI saved is always 0.02 pixels/inch smaller than the selected value, which will have a (small) effect on large image sizes. Some more discussion of this here.

这篇关于以像素为单位指定和保存具有精确大小的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆