删除已保存图像周围的空白区域
- 2025-01-06 08:32:00
- admin 原创
- 163
问题描述:
我需要拍摄一张图片,并在经过一些处理后保存。当我显示它时,该图看起来不错,但保存该图后,我在保存的图像周围出现了一些空白。我尝试了方法'tight'
的选项savefig
,也没有用。代码:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
fig = plt.figure(1)
img = mpimg.imread("image.jpg")
plt.imshow(img)
ax = fig.add_subplot(1, 1, 1)
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
plt.savefig('1.png', bbox_inches=extent)
plt.axis('off')
plt.show()
我正在尝试使用 NetworkX 在图形上绘制基本图形并保存它。我意识到没有图形它也可以工作,但是当添加图形时,保存的图像周围会出现空白;
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_edge(1, 3)
G.add_edge(1, 2)
pos = {1:[100, 120], 2:[200, 300], 3:[50, 75]}
fig = plt.figure(1)
img = mpimg.imread("image.jpg")
plt.imshow(img)
ax = fig.add_subplot(1, 1, 1)
nx.draw(G, pos=pos)
extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
plt.savefig('1.png', bbox_inches=extent)
plt.axis('off')
plt.show()
解决方案 1:
bbox_inches="tight"
您可以通过设置删除空白填充savefig
:
plt.savefig("test.png",bbox_inches='tight')
您必须将参数作为bbox_inches
字符串,也许这就是它之前对您不起作用的原因。
可能重复:
Matplotlib 图:删除轴、图例和空白
如何设置 matplotlib 图形的边距?
减少 matplotlib 图中的左右边距
解决方案 2:
我无法声称我确切地知道我的“解决方案”为何或如何起作用,但当我想将几个机翼剖面的轮廓(没有白色边缘)绘制到 PDF 文件时,我必须这样做。(请注意,我在 IPython 笔记本中使用了 matplotlib,带有 -pylab 标志。)
plt.gca().set_axis_off()
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0,
hspace = 0, wspace = 0)
plt.margins(0,0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.savefig("filename.pdf", bbox_inches = 'tight',
pad_inches = 0)
我尝试过停用此功能的不同部分,但这总是导致某处出现白色边缘。您甚至可能需要修改此功能,以防止图形边界附近的粗线因缺少边缘而被削掉。
解决方案 3:
在尝试了上述答案但没有成功(以及大量其他堆栈帖子)之后,最终对我有用的只是
plt.gca().set_axis_off()
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0,
hspace = 0, wspace = 0)
plt.margins(0,0)
plt.savefig("myfig.pdf")
重要的是,这不包括 bbox 或 padding 参数。
解决方案 4:
我从 Arvind Pereira(http://robotics.usc.edu/~ampereir/wordpress/?p=626)那里找到了一些东西,似乎对我有用:
plt.savefig(filename, transparent = True, bbox_inches = 'tight', pad_inches = 0)
解决方案 5:
最直接的方法是使用plt.tight_layout
变换,这实际上是更可取的,因为它在使用时不会进行不必要的裁剪plt.savefig
import matplotlib as plt
plt.plot([1,2,3], [1,2,3])
plt.tight_layout(pad=0)
plt.savefig('plot.png')
但是,对于需要修改图形的复杂绘图,这可能不是最佳选择。如果是这种情况,请参阅Johannes S 的回答。plt.subplots_adjust
解决方案 6:
以下函数结合了上述 johannes 的回答。我已经使用plt.figure
和plt.subplots()
多个轴对其进行了测试,效果很好。
def save(filepath, fig=None):
'''Save the current image with no whitespace
Example filepath: "myfig.png" or r"C:myfig.pdf"
'''
import matplotlib.pyplot as plt
if not fig:
fig = plt.gcf()
plt.subplots_adjust(0,0,1,1,0,0)
for ax in fig.axes:
ax.axis('off')
ax.margins(0,0)
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())
fig.savefig(filepath, pad_inches = 0, bbox_inches='tight')
解决方案 7:
这对我有用plt.savefig(save_path,bbox_inches='tight', pad_inches=0, transparent=True)
解决方案 8:
我发现以下代码非常适合这项工作。
fig = plt.figure(figsize=[6,6])
ax = fig.add_subplot(111)
ax.imshow(data)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.set_frame_on(False)
plt.savefig('data.png', dpi=400, bbox_inches='tight',pad_inches=0)
解决方案 9:
我按照这个顺序操作,效果非常好。
plt.axis("off")
fig=plt.imshow(image array,interpolation='nearest')
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
plt.savefig('destination_path.pdf',
bbox_inches='tight', pad_inches=0, format='pdf', dpi=1200)
解决方案 10:
我发现一个更简单的方法是使用plt.imsave
:
import matplotlib.pyplot as plt
arr = plt.imread(path)
plt.imsave('test.png', arr)
解决方案 11:
对于那些想要以像素而不是英寸为单位工作的人来说,这都是可行的。
此外,您还需要
from matplotlib.transforms import Bbox
然后您可以使用以下命令:
my_dpi = 100 # Good default - doesn't really matter
# Size of output in pixels
h = 224
w = 224
fig, ax = plt.subplots(1, figsize=(w/my_dpi, h/my_dpi), dpi=my_dpi)
ax.set_position([0, 0, 1, 1]) # Critical!
# Do some stuff
ax.imshow(img)
ax.imshow(heatmap) # 4-channel RGBA
ax.plot([50, 100, 150], [50, 100, 150], color="red")
ax.axis("off")
fig.savefig("saved_img.png",
bbox_inches=Bbox([[0, 0], [w/my_dpi, h/my_dpi]]),
dpi=my_dpi)
解决方案 12:
因此解决方案取决于您是否调整子图。如果您指定 plt.subplots_adjust(顶部、底部、右侧、左侧),则您不想将 bbox_inches='tight' 的 kwargs 与 plt.savefig 一起使用,因为它会自相矛盾地创建空白填充。它还允许您将图像保存为与输入图像相同的尺寸(600x600 输入图像保存为 600x600 像素输出图像)。
如果您不关心输出图像大小的一致性,您可以省略 plt.subplots_adjust 属性,只需将 bbox_inches='tight' 和 pad_inches=0 kwargs 与 plt.savefig 一起使用。
此解决方案适用于 matplotlib 版本 3.0.1、3.0.3 和 3.2.1。当您有多个子图时,它也有效(例如 plt.subplots(2,2,...)。
def save_inp_as_output(_img, c_name, dpi=100):
h, w, _ = _img.shape
fig, axes = plt.subplots(figsize=(h/dpi, w/dpi))
fig.subplots_adjust(top=1.0, bottom=0, right=1.0, left=0, hspace=0, wspace=0)
axes.imshow(_img)
axes.axis('off')
plt.savefig(c_name, dpi=dpi, format='jpeg')
解决方案 13:
在 Jupyter 笔记本中,可以添加以下行:
%config InlineBackend.print_figure_kwargs = {'pad_inches':0}
这是一个最小的例子
import matplotlib.pyplot as plt
import numpy as np
%config InlineBackend.print_figure_kwargs = {'pad_inches':0}
fig, ax = plt.subplots()
ax.axis("off")
ax.imshow(np.fromfunction(lambda i, j: np.sin(j), (15, 15)), cmap="YlGnBu")
解决方案 14:
你可以试试这个。它解决了我的问题。
import matplotlib.image as mpimg
img = mpimg.imread("src.png")
mpimg.imsave("out.png", img, cmap=cmap)
解决方案 15:
我通常需要从 PDF 文件中裁剪空白以用于科学论文。调整 matplotlib 以真正删除所有空白从未完美起作用,并且会保留一些空白……如果您使用 pdflatex,则随附的 pdfcrop 可执行文件非常有用,可以删除导出图形中的空白。因此,在导出 PDF 后,pdfcrop 会删除剩余的空白。这是一个 Python 代码片段,用于使用 pdfcrop 自动裁剪您保存的图形。只需调用函数而不是 plt.savefig:
import subprocess
import os
def save_and_crop(path, *args, **kwargs):
filename, file_extension = os.path.splitext(path)
plt.savefig(path, *args, **kwargs)
if file_extension == ".pdf":
cropped_path = filename + "_cropped" + file_extension
subprocess.run(["pdfcrop", path, cropped_path], stdout=subprocess.DEVNULL)
解决方案 16:
这对我有用,可以保存用 imshow 绘制的 numpy 数组到文件
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(10,10))
plt.imshow(img) # your image here
plt.axis("off")
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0,
hspace = 0, wspace = 0)
plt.savefig("example2.png", box_inches='tight', dpi=100)
plt.show()
扫码咨询,免费领取项目管理大礼包!