![image w/o white space](https://istack.dev59.com/Z3y0t.webp)
我尝试了以下帖子,但仍然无法完全去除白边,就像示例图像中那样。请问有人能给我一些指导吗?谢谢! 这是我的图片:
![my image](https://istack.dev59.com/nSasT.webp)
def plot_im(array=None, ind=0):
"""A function to plot the image given a images matrix, type of the matrix: \
either original or fool, and the order of images in the matrix"""
img_reshaped = array[ind, :].reshape((28, 28))
imgplot = plt.imshow(img_reshaped)
# Output as a grid of 10 rows and 3 cols with first column being original, second being
# delta and third column being adversaril
nrow = 10
ncol = 3
n = 0
from matplotlib import gridspec
fig = plt.figure(figsize=(30, 30))
gs = gridspec.GridSpec(nrow, ncol, width_ratios=[1, 1, 1])
for row in range(nrow):
for col in range(ncol):
plt.subplot(gs[n])
if col == 0:
#plt.subplot(nrow, ncol, n)
plot_im(array=images_2, ind=row)
elif col == 1:
#plt.subplot(nrow, ncol, n)
plt.imshow(w_delta)
else:
#plt.subplot(nrow, ncol, n)
plot_im(array=images_fool, ind=row)
n += 1
plt.tight_layout()
#plt.show()
plt.savefig('grid_figure.pdf')
figsize=(4, 10)
更多是因为想法是,对于有n行和m列的情况,一个(m+1, n)的figsize可能是合适的;其余的则通过微调subplot参数来完成。 - ImportanceOfBeingErnestfigsize
是图形的尺寸,单位为英寸。我已经编辑了答案,包括一个示例,其中参数是从列数和行数计算出来的。 - ImportanceOfBeingErnestgen = iter(image_array)
放在 for 循环之外,ax.imshow(next(gen, np.full((1, 1, 3), 255)))
放在循环内。当gen
没有值时,它会渲染一个大小为 1x1 的白色图像。 - DharmaTurtle