【问题标题】:How to revise the plt.subplot code to single image? ( In Github GAN code)如何将 plt.subplot 代码修改为单个图像? (在 Github GAN 代码中)
【发布时间】:2019-05-01 08:48:22
【问题描述】:

我正在使用来自 github 的代码 https://github.com/eriklindernoren/Keras-GAN/blob/master/gan/gan.py

The demo code show 25 generated image in one single image file. 但我想将原始大小的每张图像打印为 png 文件。我尝试了几种方法,例如

plt.imshow()

cv2.imwrite()

但是,他们没有工作。如果没有子图图像,我无法打印正确的图像。

这是打印图像的部分:

def sample_images(self, epoch):
    r, c = 5, 5
    noise = np.random.normal(0, 1, (r * c, self.latent_dim))
    gen_imgs = self.generator.predict(noise)

    # Rescale images 0 - 1
    gen_imgs = 0.5 * gen_imgs + 0.5
    fig, axs = plt.subplots(r, c)
    cnt = 0

    for i in range(r):
        for j in range(c):
            axs[i,j].imshow(gen_imgs[cnt, :,:,0], cmap='gray')
            axs[i,j].axis('off')
            cnt += 1
    fig.savefig("images/%d.png" % epoch)
    plt.close()

非常感谢。

【问题讨论】:

    标签: python matplotlib subplot


    【解决方案1】:

    您正在使用

    创建一个包含 25 个子图的图形
    fig, axs = plt.subplots(5, 5)
    

    将其替换为fig, ax = plt.subplots() 以创建具有一组轴的图形。如果您希望 25 个图像中的每一个都在它自己的图中,这也需要进入循环内部。此外,您还需要将对savefig 的调用移动到循环中:

    def sample_images(self, epoch):
        r, c = 5, 5
        noise = np.random.normal(0, 1, (r * c, self.latent_dim))
        gen_imgs = self.generator.predict(noise)
    
        # Rescale images 0 - 1
        gen_imgs = 0.5 * gen_imgs + 0.5
    
        cnt = 0
    
        for i in range(r):
            for j in range(c):
                fig, ax = plt.subplots()
                ax.imshow(gen_imgs[cnt, :,:,0], cmap='gray')
                ax.axis('off')
                cnt += 1
                fig.savefig("images/%d.png" % epoch)
                plt.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-21
      • 2022-12-10
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      • 2011-06-24
      • 2021-05-17
      • 1970-01-01
      相关资源
      最近更新 更多