{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Basic packages and libraries used","metadata":{}},{"cell_type":"code","source":"import os # to navigate and manipulate directories.\nimport cv2 # to read, resize and save images.","metadata":{"execution":{"iopub.status.busy":"2022-03-17T01:17:51.265386Z","iopub.execute_input":"2022-03-17T01:17:51.265749Z","iopub.status.idle":"2022-03-17T01:17:51.618185Z","shell.execute_reply.started":"2022-03-17T01:17:51.265691Z","shell.execute_reply":"2022-03-17T01:17:51.617315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Directory preparation in Kaggle Kernel","metadata":{}},{"cell_type":"markdown","source":"First step is creating a folder to save the resized images within Kaggle Kernel output directory.","metadata":{}},{"cell_type":"code","source":"os.makedirs('./resized_train_images')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T23:18:59.414044Z","iopub.execute_input":"2022-03-16T23:18:59.414351Z","iopub.status.idle":"2022-03-16T23:18:59.420214Z","shell.execute_reply.started":"2022-03-16T23:18:59.414322Z","shell.execute_reply":"2022-03-16T23:18:59.419476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('./resized_test_images')","metadata":{"execution":{"iopub.status.busy":"2022-03-17T01:17:54.591226Z","iopub.execute_input":"2022-03-17T01:17:54.591491Z","iopub.status.idle":"2022-03-17T01:17:54.596282Z","shell.execute_reply.started":"2022-03-17T01:17:54.591464Z","shell.execute_reply":"2022-03-17T01:17:54.595297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now, lets just assign the directories to path variables.","metadata":{}},{"cell_type":"code","source":"TRAIN_IMAGES_DIR = '../input/happy-whale-and-dolphin/train_images'\nTEST_IMAGES_DIR = '../input/happy-whale-and-dolphin/test_images'\nRESIZED_TRAIN_IMAGES_DIR = './resized_train_images'\nRESIZED_TEST_IMAGES_DIR = './resized_test_images'","metadata":{"execution":{"iopub.status.busy":"2022-03-17T01:19:24.992924Z","iopub.execute_input":"2022-03-17T01:19:24.993208Z","iopub.status.idle":"2022-03-17T01:19:24.997799Z","shell.execute_reply.started":"2022-03-17T01:19:24.993180Z","shell.execute_reply":"2022-03-17T01:19:24.997080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Function to resize and save images","metadata":{}},{"cell_type":"markdown","source":"The function bellow will help to run and debug the resizing and saving operation if needed.","metadata":{}},{"cell_type":"code","source":"def resize_and_save(source_dir, destination_dir, resize_to, gray_scale=False):\n    for dirpath, dirnames, filenames in os.walk(source_dir):\n        num_images = len(filenames)\n        print('Found:', num_images, 'files in:', dirpath)\n        \n        print('Start of execution')\n        counter = 0\n        for name in filenames:\n            original_image_path = os.path.join(dirpath, name)\n            \n            if gray_scale:\n                original_image = cv2.imread(original_image_path, cv2.IMREAD_GRAYSCALE)\n            else:\n                original_image = cv2.imread(original_image_path)\n            \n            resized_image = cv2.resize(original_image, (resize_to, resize_to))\n            \n            cv2.imwrite(os.path.join(destination_dir, name), resized_image)\n            \n            counter += 1\n            \n            if (counter % 1000) == 0:\n                print('Saved', counter, 'of', num_images, 'images')\n                \n    print('End of execution')","metadata":{"execution":{"iopub.status.busy":"2022-03-17T01:18:20.735259Z","iopub.execute_input":"2022-03-17T01:18:20.735567Z","iopub.status.idle":"2022-03-17T01:18:20.744553Z","shell.execute_reply.started":"2022-03-17T01:18:20.735533Z","shell.execute_reply":"2022-03-17T01:18:20.743428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Running the resizing function","metadata":{}},{"cell_type":"markdown","source":"Now, just need to run our `resize_and_save` function for the train and test images.","metadata":{}},{"cell_type":"code","source":"resize_and_save(TRAIN_IMAGES_DIR, RESIZED_TRAIN_IMAGES_DIR, 256, gray_scale=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T23:25:39.348707Z","iopub.execute_input":"2022-03-16T23:25:39.348982Z","iopub.status.idle":"2022-03-17T00:00:21.544846Z","shell.execute_reply.started":"2022-03-16T23:25:39.348953Z","shell.execute_reply":"2022-03-17T00:00:21.543233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resize_and_save(TEST_IMAGES_DIR, RESIZED_TEST_IMAGES_DIR, 256, gray_scale=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-17T01:19:39.883681Z","iopub.execute_input":"2022-03-17T01:19:39.884335Z","iopub.status.idle":"2022-03-17T01:39:22.816117Z","shell.execute_reply.started":"2022-03-17T01:19:39.884287Z","shell.execute_reply":"2022-03-17T01:39:22.814990Z"},"trusted":true},"execution_count":null,"outputs":[]}]}