Home > OS >  How to save output images after applying tensorflow.keras.preprocessing to images?
How to save output images after applying tensorflow.keras.preprocessing to images?

Time:01-05

How can I save the preprocessed images (after the img_height and img_width preprocessing has been applied) in a folder for me to view them?

This is my code for preprocessing images from a directory. My model trains on these preprocessed images.

def evaluate(image):
  batch_size = 32
  img_height = 180
  img_width = 180
  img = keras.preprocessing.image.load_img(
  image,
  target_size=(img_height, img_width),
  interpolation = "bilinear",
  color_mode = 'rgb'
  )

CodePudding user response:

Code below has 2 functions. Save_images function reads in images from the source_dir, preprocesses them and saves them to the save_to_dir. NOTE: if you make the save_to_dir the same as the source_dir, files in the source directory will be over written. If the save_to_dir does not exist it is created. If it exists it is checked for content. If it has files in it you are asked if you wish to delete the files, Halt the function or continue. The view_images function displays all the images present in the view_dir

import os
import shutil
from PIL import Image
import tensorflow as tf
import matplotlib.pyplot as plt

def save_images(source_dir, save_to_dir, height,width):
    # function reads in image files from source_dir, preprocess the images and saves them to save_to_dir
    if os.path.isdir(save_to_dir)== True: # if the directory exists
        print('directory ', save_to_dir, ' has images in it')
        ans=input('Enter C to continue, D to delete existing files or H to halt')
        if ans == 'd' or ans == 'D':
            shutil.rmtree(save_to_dir)
            os.mkdir(save_to_dir)
        elif ans == 'h' or ans == 'H':
            print ('Execution terminated by user input')
            return        
    else:
        os.mkdir(save_to_dir)
    flist=os.listdir(source_dir)
    for f in flist:
        fpath=os.path.join(source_dir,f)
        img=tf.keras.preprocessing.image.load_img( fpath, target_size=(height, width),
                                                  interpolation = "bilinear",  color_mode = 'rgb')        
        fname=os.path.split(fpath)[1]
        dest_path=os.path.join(save_to_dir, fname)
        img.save(dest_path)
    print(len(flist), ' files were saved to ', save_to_dir)
  
def view_images(view_dir):
    # function displays all images in the view_dir with titles as the filename
    flist=os.listdir(view_dir)    
    fcount=len(flist)    
    rows= fcount//6   1    
    plt.figure(figsize=(20,rows*5))
    for i,f in enumerate(flist):        
        fpath=os.path.join(view_dir,f)        
        img=plt.imread(fpath)
        plt.subplot(rows, 6, i   1)
        plt.title(f, color='yellow', fontsize=16)
        plt.imshow(img)
    plt.show()
# Example of use    
source_dir=r'C:\Temp\butterflies\detector'
#NOTE if you make the save_to_dir the same as source_dir the original files will be over written
save_to_dir=r'C:\Temp\butterflies\storage'
height=100
width = 100
save_images(source_dir, save_to_dir, height,width)    
view_images(save_to_dir)   
  •  Tags:  
  • Related