{"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":"code","source":"print(\"Please upvote :)\")","metadata":{"execution":{"iopub.status.busy":"2022-04-03T16:53:29.218935Z","iopub.execute_input":"2022-04-03T16:53:29.219579Z","iopub.status.idle":"2022-04-03T16:53:29.224522Z","shell.execute_reply.started":"2022-04-03T16:53:29.219531Z","shell.execute_reply":"2022-04-03T16:53:29.223587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom PIL import Image\nfrom tqdm import tqdm\nimport shutil","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-03T16:53:29.237445Z","iopub.execute_input":"2022-04-03T16:53:29.238081Z","iopub.status.idle":"2022-04-03T16:53:29.242314Z","shell.execute_reply.started":"2022-04-03T16:53:29.238041Z","shell.execute_reply":"2022-04-03T16:53:29.241526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_size = 224\ntarget_size","metadata":{"execution":{"iopub.status.busy":"2022-04-06T20:23:24.615484Z","iopub.execute_input":"2022-04-06T20:23:24.616375Z","iopub.status.idle":"2022-04-06T20:23:24.644055Z","shell.execute_reply.started":"2022-04-06T20:23:24.616272Z","shell.execute_reply":"2022-04-06T20:23:24.643336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"directory = \"../input/happy-whale-and-dolphin/train_images\"\nfilenames = os.listdir(directory)\nfilenames[:5], len(filenames)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T16:53:29.25704Z","iopub.execute_input":"2022-04-03T16:53:29.257496Z","iopub.status.idle":"2022-04-03T16:53:29.293229Z","shell.execute_reply.started":"2022-04-03T16:53:29.257457Z","shell.execute_reply":"2022-04-03T16:53:29.292368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#filenames = filenames[:99]","metadata":{"execution":{"iopub.status.busy":"2022-04-03T16:53:29.294913Z","iopub.execute_input":"2022-04-03T16:53:29.295197Z","iopub.status.idle":"2022-04-03T16:53:29.298936Z","shell.execute_reply.started":"2022-04-03T16:53:29.295166Z","shell.execute_reply":"2022-04-03T16:53:29.297943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder = \"happywhale_resized_images\"\nos.makedirs(folder, exist_ok=True)\n\nfor i, filename in tqdm(enumerate(filenames), total=len(filenames)):\n    filepath = os.path.join(directory, filename)\n    image = Image.open(filepath)\n    original_image_size = image.size\n    image_size = tuple(\n        [\n            round(dimension * target_size / max(original_image_size))\n            for dimension in original_image_size\n        ]\n    )\n    image = image.resize(image_size)\n    new_image = Image.new(\"RGB\", (target_size, target_size))\n    new_image.paste(\n        image, ((target_size - image_size[0]) // 2, (target_size - image_size[1]) // 2)\n    )\n    new_filepath = os.path.join(folder, filename)\n    new_image.save(new_filepath)\n\n    if i < 9:\n        display(new_image.size)\n        display(new_image)","metadata":{"execution":{"iopub.status.busy":"2022-04-03T16:53:29.30013Z","iopub.execute_input":"2022-04-03T16:53:29.300352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tar -czf {folder}.tar.gz {folder}","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree(folder)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}