gpt4 book ai didi

python - matplotlib.pyplot : remove white space and keep same size while saving the image

转载 作者:行者123 更新时间:2023-12-01 05:08:06 30 4
gpt4 key购买 nike

我知道有些人以前问过这个问题,但他们提供的解决方案似乎对我不起作用。我只是提供下面的链接,也许它们对您有帮助。

Matplotlib plots: removing axis, legends and white spaces

How to set the margins for a matplotlib figure?

如问题标题所述,我使用以下代码来处理此问题。

plt.imshow(img)
plt.axis('off')
plt.savefig('seg_missi5.png', bbox_inches='tight')
plt.show()

我希望保存的图像没有空格,并且大小与原始图像相同。

我尝试使用 print img.shape 来查看 img 的大小,它显示 (342, 548, 3),其中与原始图像相同,但保存的图像却不同。它的大小为 438 x 675,更大并且有空格。

最佳答案

可以说,matplotlib 可能不是实现这一目标的最佳工具(尽管它当然有可能)。我建议Python Image Library作为替补。使用这个库,您可以像这样保存图像:

from PIL import Image

im = Image.fromarray(np.uint8(img))
im.save('outfile.png', "PNG")

关于python - matplotlib.pyplot : remove white space and keep same size while saving the image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24709023/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com