# 读取图片
plt.imshow(data[15][0])
plt.axis("off")
# 去除图像周围的白边
height, width, channels = img.shape
# 如果dpi=300,那么图像大小=height*width
fig.set_size_inches(width/100.0/3.0, height/100.0/3.0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1,bottom=0,left=0,right=1,hspace=0,wspace=0)
plt.margins(0,0)
plt.savefig("./imgs/xxx.png", bbox_inches="tight", pad_inches=0.0)
其中 bbox_inches = 'tight' 可以去除坐标轴占用的空间(如果只是set_visiable = False,只能不显示,但空间还占用)
dpi指定为原图的dpi,控制保存图片的质量/分辨率
pad_inches = 0 这句是重点,去除所有白边