【问题标题】:saving a grid of heterogenous images in Python在 Python 中保存异构图像网格
【发布时间】:2019-04-14 18:38:31
【问题描述】:

如何使用类似下面的行将图像保存在 4x4 的异构图像网格上?假设图像由 sample[i] 识别,i 取 16 个不同的值。

scipy.misc.imsave(str(img_index) + '.png', sample[1])

与此答案类似,但适用于 16 个不同的图像 https://stackoverflow.com/a/42041135/2414957

只要能做到,我不会偏向于使用的方法。此外,我对保存图像感兴趣,而不是使用 plt.show() 显示它们,因为我正在使用远程服务器并处理 CelebA 图像数据集,这是一个巨大的数据集。我只想从我的批次中随机选择 16 张图像并保存 DCGAN 的结果,看看它是否有意义或是否收敛。

*目前,我正在保存如下图片:

batch_no = random.randint(0, 63)


scipy.misc.imsave('sample_gan_images/iter_%d_epoch_%d_sample_%d.png' %(itr, epoch, batch_no), sample[batch_no])

在这里,我有 25 个 epoch 和 2000 次迭代,批量大小为 64。

【问题讨论】:

  • @Panlantic82 这到底是什么??? 987654322@(以前称为“善待政策”)
  • 等等,你链接的答案有什么问题?它似乎做你想做的一切。要保存而不是显示,只需使用 plt.savefig 代替 plt.show
  • @U9-Forward 有很多逗号。只需标记以引起版主注意并解释情况要简单得多。他们将确定约束用户的最佳方式。我已经这样做了。
  • @coldspeed 感谢您告诉我,但是在哪里举报?
  • @U9-Forward 如果答案已经被删除,您需要等到 10k rep 才能看到已删除的内容。

标签: python image numpy matplotlib scipy


【解决方案1】:

我在github上找到了这个,也分享一下:

import matplotlib.pyplot as plt

def merge_images(image_batch, size):
    h,w = image_batch.shape[1], image_batch.shape[2]
    c = image_batch.shape[3]
    img = np.zeros((int(h*size[0]), w*size[1], c))
    for idx, im in enumerate(image_batch):
        i = idx % size[1]
        j = idx // size[1]
        img[j*h:j*h+h, i*w:i*w+w,:] = im
    return img

im_merged = merge_images(sample, [8,8])
plt.imsave('sample_gan_images/im_merged.png', im_merged )

【讨论】:

    【解决方案2】:

    就个人而言,我倾向于使用matplotlib.pyplot.subplots 来处理这些情况。如果您的图像确实是异构的,那么它可能是比您链接到的答案中基于图像连接的方法更好的选择。

    import matplotlib.pyplot as plt
    from scipy.misc import face
    
    x = 4
    y = 4
    
    fig,axarr = plt.subplots(x,y)
    ims = [face() for i in range(x*y)]
    
    for ax,im in zip(axarr.ravel(), ims):
        ax.imshow(im)
    
    fig.savefig('faces.png')
    

    我对@9​​87654328@ 最大的抱怨是结果图中的空白数量。同样,对于您的应用程序,您可能不需要轴刻度/帧。这是处理这些问题的包装函数:

    import matplotlib.pyplot as plt
    
    def savegrid(ims, rows=None, cols=None, fill=True, showax=False):
        if rows is None != cols is None:
            raise ValueError("Set either both rows and cols or neither.")
    
        if rows is None:
            rows = len(ims)
            cols = 1
    
        gridspec_kw = {'wspace': 0, 'hspace': 0} if fill else {}
        fig,axarr = plt.subplots(rows, cols, gridspec_kw=gridspec_kw)
    
        if fill:
            bleed = 0
            fig.subplots_adjust(left=bleed, bottom=bleed, right=(1 - bleed), top=(1 - bleed))
    
        for ax,im in zip(axarr.ravel(), ims):
            ax.imshow(im)
            if not showax:
                ax.set_axis_off()
    
        kwargs = {'pad_inches': .01} if fill else {}
        fig.savefig('faces.png', **kwargs)
    

    在之前使用的同一组图像上运行 savegrid(ims, 4, 4) 会产生:

    如果您使用savegrid,如果您希望每个单独的图像占用更少的空间,请传递fill=False 关键字arg。如果要显示坐标轴刻度/帧,请传递 showax=True

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      相关资源
      最近更新 更多