Resize Images in Image Set and Save to New Location
Python
For images stored in a folder with filenames or IDs referenced in an accompanying csv file. Take all images, resize them and then save to a new folder.
1| import pandas as pd 2| from tqdm import tqdm 3| from PIL import Image 4| 5| #Location of original images 6| train_path = 'data/raw/train_images/' 7| #Destination folder for resized images (folder must already be created) 8| train_dest_path = 'data/processed/train_resized_images/' 9| 10| #Set new size 11| NEW_SIZE = (255,255) 12| 13| #File that contains data on images. Image filename is contained in image_id field 14| train = pd.read_csv('data/raw/train.csv') 15| 16| #Resize images and save to destination folder 17| def resize_images(df, path, dest_path): 18| for i in tqdm(range(df.shape[0])): 19| im = Image.open(path + df['image_id'][i]) 20| im = im.resize(NEW_SIZE) 21| im.save(dest_path + df['image_id'][i]) 22| resize_images(train,train_path,train_dest_path)
133
121
117
109