2024-06-06 17:34:22 发布
网友
在numpy/scipy中,我有一个存储在数组中的图像。我可以显示它,我想使用savefig保存它,而不使用任何边框、轴、标签、标题,。。。只是单纯的形象,没有别的。
savefig
我想避免像PyPNG或scipy.misc.imsave这样的软件包,它们有时是有问题的(它们并不总是安装得很好,对我来说只有基本的savefig()
PyPNG
scipy.misc.imsave
savefig()
可以在轴内找到图像的bbox(使用get_window_extent),并使用bbox_inches参数仅保存图像的该部分:
get_window_extent
bbox_inches
import numpy as np import matplotlib.pyplot as plt data=np.arange(9).reshape((3,3)) fig=plt.figure() ax=fig.add_subplot(1,1,1) plt.axis('off') plt.imshow(data) extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted()) plt.savefig('/tmp/test.png', bbox_inches=extent)
我从乔·金顿那里学会了这个把戏。
编辑
将aspect='normal更改为aspect='auto',因为在最近的matplotlib版本中发生了更改(感谢@Luke19)。
aspect='normal
aspect='auto'
假设:
import matplotlib.pyplot as plt
制作没有框架的图形:
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(your_image, aspect='auto') fig.savefig(fname, dpi)
aspect参数更改像素大小,以确保它们填充在fig.set_size_inches(…)中指定的图形大小。要了解如何处理这类事情,请通读matplotlib's documentation,特别是有关轴、轴和艺术家的主题。
aspect
fig.set_size_inches(…)
一个更简单的解决方案似乎是:
fig.savefig('out.png', bbox_inches='tight', pad_inches=0)
可以在轴内找到图像的bbox(使用
get_window_extent
),并使用bbox_inches
参数仅保存图像的该部分:我从乔·金顿那里学会了这个把戏。
编辑
将
aspect='normal
更改为aspect='auto'
,因为在最近的matplotlib版本中发生了更改(感谢@Luke19)。假设:
制作没有框架的图形:
使内容填满整个数字
然后在上面画出你的形象:
aspect
参数更改像素大小,以确保它们填充在fig.set_size_inches(…)
中指定的图形大小。要了解如何处理这类事情,请通读matplotlib's documentation,特别是有关轴、轴和艺术家的主题。一个更简单的解决方案似乎是:
相关问题 更多 >
编程相关推荐