Home > OS >  Save GAN generated images one by one
Save GAN generated images one by one

Time:03-15

I have generated some images from the Fashion Mnist dataset, However, I am not able to come up with a function or the way to save each image as a single file. I only have found a way to save them in groups. Can someone help me on how to save images one by one?

This is what I have for the moment:

def generate_and_save_images(model, 
     epoch,test_input):
     predictions = model(test_input, training=False)

     fig = plt.figure(figsize=(4,4))

    for i in range(predictions.shape[0]):
        plt.subplot(4, 4, i 1)
        plt.imshow(predictions[i, :, :, 0] * 127.5   
        127.5, cmap='gray')
        plt.axis('off')

    plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))

    plt.show()

CodePudding user response:

Try using plt.imsave to save each image separately:

def generate_and_save_images(model, epoch, test_input):
  predictions = model(test_input, training=False)
  fig = plt.figure(figsize=(4, 4))

  for i in range(predictions.shape[0]):
      plt.subplot(4, 4, i 1)
      plt.imshow(predictions[i, :, :, 0] * 127.5   127.5, cmap='gray')
      plt.imsave('image_at_epoch_{:04d}-{}.png'.format(epoch, i), predictions[i, :, :, 0] * 127.5   127.5, cmap='gray')
      plt.axis('off')

  plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))
  plt.show()
  • Related