删除已保存图像周围的空白区域

编程入门 行业动态 更新时间:2024-10-28 21:18:06
本文介绍了删除已保存图像周围的空白区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要拍摄图像并在经过一些过程后保存.当我显示该图时,该图看起来不错,但是在保存该图后,我在保存的图像周围有一些空白.我已经为 savefig 方法尝试了 'tight' 选项,但也没有用.代码:

I need to take an image and save it after some process. The figure looks fine when I display it, but after saving the figure, I got some white space around the saved image. I have tried the 'tight' option for savefig method, did not work either. The code:

import matplotlib.image as mpimg import matplotlib.pyplot as plt fig = plt.figure(1) img = mpimg.imread(path) 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 绘制基本图形并保存.我意识到没有图形它也能工作,但是当添加图形时,我在保存的图像周围得到了空白;

I am trying to draw a basic graph by using NetworkX on a figure and save it. I realized that without a graph it works, but when added a graph I get white space around the saved image;

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()

推荐答案

我不能声称我确切地知道为什么或如何我的解决方案"有效,但是当我想绘制几个翼型截面的轮廓时,这是我必须做的没有白边—到 PDF 文件.(请注意,我在 IPython 笔记本中使用了 matplotlib,并带有 -pylab 标志.)

I cannot claim I know exactly why or how my “solution” works, but this is what I had to do when I wanted to plot the outline of a couple of aerofoil sections — without white margins — to a PDF file. (Note that I used matplotlib inside an IPython notebook, with the -pylab flag.)

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)

我曾尝试停用此功能的不同部分,但这总是会导致某处出现白边.您甚至可以修改此设置,以保持靠近图形边界的粗线不会因缺少边距而被刮掉.

I have tried to deactivate different parts of this, but this always lead to a white margin somewhere. You may even have modify this to keep fat lines near the limits of the figure from being shaved by the lack of margins.

更多推荐

删除已保存图像周围的空白区域

本文发布于:2023-07-18 05:05:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141269.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   空白   区域

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!