# 加载相关库及图片
from google.colab import drive
import matplotlib.pyplot as plt
import numpy as np
drive.mount('/content/drive')
from glob import glob
mask_pth=glob('/content/drive/My Drive/Colab Notebooks/data_nifti/rgb_B2_patch02/PNG/'+'*.png')[3]
## plt保存去除白边 并且按照pixel*pixel格式输出 (figsize和dpi共同控制)
# fig = plt.gcf()
# fig.set_size_inches(16,16) #dpi = 100, output = 1600*1600 pixels inch (width,height)=(16,16)
plt.figure(figsize=(16,16),dpi=100)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)
plt.margins(0,0)
mask_ins=plt.imread(mask_pth)
plt.imshow(mask_ins)
plt.savefig('/content/example.png',pad_inches=0) # dpi=100 和上文相对应 pixel尺寸/dpi=inch尺寸
plt.show() # 此处顺序不能弄反 imshow(),savefig(),show()
#plt.clf() #plt.clf()的作用:用于批量存储图片时 每一次显示图片并保存以后,释放图窗,接受下一个图片显示和存储
此段代码的主要作用:
1)消除plt图片保存时的白边
2)按照像素尺寸和dpi分辨率要求,保存图片
最终图片效果: