Jak*_* M. 72 python numpy image matplotlib scipy
在numpy/scipy中,我有一个存储在数组中的图像.我可以显示它,我想使用savefig
没有任何边框,轴,标签,标题保存它...只是纯粹的图像,没有别的.
我想避免使用PyPNG
或者包装scipy.misc.imsave
,它们有时会出现问题(它们并不总是安装得很好,savefig()
对我来说只是基本的
mat*_*hat 99
假设 :
import matplotlib.pyplot as plt
Run Code Online (Sandbox Code Playgroud)
制作没有框架的图形:
fig = plt.figure(frameon=False)
fig.set_size_inches(w,h)
Run Code Online (Sandbox Code Playgroud)
使内容填满整个数字
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
Run Code Online (Sandbox Code Playgroud)
然后在上面绘制图像:
ax.imshow(your_image, aspect='normal')
fig.savefig(fname, dpi)
Run Code Online (Sandbox Code Playgroud)
该aspect
参数更改像素大小以确保它们填充指定的图形大小fig.set_size_inches(…)
.要了解如何使用这类内容,请阅读matplotlib的文档,特别是关于Axes,Axis和Artist的主题.
wea*_*rog 58
一个更简单的解决方案似乎是:
fig.savefig('out.png', bbox_inches='tight', pad_inches=0)
Run Code Online (Sandbox Code Playgroud)
unu*_*tbu 24
您可以在轴内找到图像的bbox(使用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)
Run Code Online (Sandbox Code Playgroud)
我学会了从乔金顿这一招在这里.
小智 15
我在我的案例中尝试了几个选项,最好的解决方案是:
fig.subplots_adjust(bottom = 0)
fig.subplots_adjust(top = 1)
fig.subplots_adjust(right = 1)
fig.subplots_adjust(left = 0)
Run Code Online (Sandbox Code Playgroud)
然后保存你的身材 savefig
pli*_*mid 10
实际上我最近尝试过这个,而不是所有这些行,你可以使用
plt.imsave(image_path, image)
Run Code Online (Sandbox Code Playgroud)
奇迹般有效。只需一行,问题就解决了。
imsave()
Run Code Online (Sandbox Code Playgroud)
文档(https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.imsave.html)
小智 8
我会建议heron13回答,从这里借来一点点添加,以便在将bbox设置为紧密模式后删除左边的填充,因此:
axes = fig.axes()
axes.get_xaxis().set_visible(False)
axes.get_yaxis().set_visible(False)
fig.savefig('out.png', bbox_inches='tight', pad_inches=0)
Run Code Online (Sandbox Code Playgroud)
小智 8
这个对我有用
plt.savefig('filename',bbox_inches='tight',transparent=True, pad_inches=0)
Run Code Online (Sandbox Code Playgroud)
对于任何试图在 Jupyter 中执行此操作的人
plt.axis('off')
spec = plt.imshow
plt.savefig('spec',bbox_inches='tight',transparent=True, pad_inches=0)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
54379 次 |
最近记录: |