Saturday, 12 September 2020

HOW TO RESIZE IMAGES AND AND SAVE IN DIRECTORY USING KERAS IMAGE

 


from keras.preprocessing import image
import os

for i in train["unique_id/name of image "]:
  img_path = os.path.join(path,i)
  img = image.load_img(img_path,target_size=(224,224))
  image.save_img(f"Images_resize224/{i}",img)

#make directories where one two .. is class name
dire = ['one','two','three','four','five','six','seven'] for d in dire: os.mkdir(d)

# to track number of images per class let say 150
growth_stage_count = {}
for d in range(len(dire)):
    growth_stage_count.update({d+1:0})
    
#copy 224*244 images in respective directories for i,v in enumerate (train["UID"]): gs = train['growth_stage'].iloc[i] growth_stage_count[gs]+=1 if growth_stage_count[gs] <=150: v = v+'.jpeg' img_path = os.path.join('Images/',v) print(img_path) img = image.load_img(img_path,target_size=(224,224)) image.save_img(f"{dire[gs-1]}/{v}.jpeg",img)

No comments:

Post a Comment