{"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":"# Convert tif data into jpg\nThe code below load the tif images and resamples them to 10% of the orignal size in jpg format.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom PIL import Image\nimport cv2\nimport tifffile as tifi\nimport gc\nfrom tqdm import tqdm","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Paths\n\nSAMPLE_SUBMISSION_PATH = '/kaggle/input/mayo-clinic-strip-ai/sample_submission.csv'\n\nTRAIN_META_DATA_PATH = '/kaggle/input/mayo-clinic-strip-ai/train.csv'\nTRAIN_IMAGES_PATH = '/kaggle/input/mayo-clinic-strip-ai/train'\n\nTEST_META_DATA_PATH = '/kaggle/input/mayo-clinic-strip-ai/test.csv'\nTEST_IMAGES_PATH = '/kaggle/input/mayo-clinic-strip-ai/test'\n\nOTHER_META_DATA_PATH = '/kaggle/input/mayo-clinic-strip-ai/other.csv'\nOTHER_IMAGES_PATH = '/kaggle/input/mayo-clinic-strip-ai/other'","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:12.343098Z","iopub.execute_input":"2022-07-12T13:27:12.343914Z","iopub.status.idle":"2022-07-12T13:27:12.348827Z","shell.execute_reply.started":"2022-07-12T13:27:12.343881Z","shell.execute_reply":"2022-07-12T13:27:12.347975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(SAMPLE_SUBMISSION_PATH).copy()\nprint(sample_submission.shape)\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:12.698756Z","iopub.execute_input":"2022-07-12T13:27:12.699179Z","iopub.status.idle":"2022-07-12T13:27:12.731634Z","shell.execute_reply.started":"2022-07-12T13:27:12.699143Z","shell.execute_reply":"2022-07-12T13:27:12.730569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta_data = pd.read_csv(TRAIN_META_DATA_PATH).copy()\nprint(train_meta_data.shape)\ntrain_meta_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:12.86191Z","iopub.execute_input":"2022-07-12T13:27:12.862315Z","iopub.status.idle":"2022-07-12T13:27:12.884145Z","shell.execute_reply.started":"2022-07-12T13:27:12.86228Z","shell.execute_reply":"2022-07-12T13:27:12.882985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_meta_data = pd.read_csv(TEST_META_DATA_PATH)\nprint(test_meta_data.shape)\ntest_meta_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:13.361832Z","iopub.execute_input":"2022-07-12T13:27:13.362265Z","iopub.status.idle":"2022-07-12T13:27:13.378826Z","shell.execute_reply.started":"2022-07-12T13:27:13.362219Z","shell.execute_reply":"2022-07-12T13:27:13.378065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"other_meta_data = pd.read_csv(OTHER_META_DATA_PATH)\nprint(other_meta_data.shape)\nother_meta_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:13.882011Z","iopub.execute_input":"2022-07-12T13:27:13.882394Z","iopub.status.idle":"2022-07-12T13:27:13.902076Z","shell.execute_reply.started":"2022-07-12T13:27:13.882363Z","shell.execute_reply":"2022-07-12T13:27:13.900972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image(image_path):\n    img = tifi.imread(image_path)\n    img = img[::10,::10]\n    img = Image.fromarray(img)\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:14.306517Z","iopub.execute_input":"2022-07-12T13:27:14.30691Z","iopub.status.idle":"2022-07-12T13:27:14.312791Z","shell.execute_reply.started":"2022-07-12T13:27:14.306874Z","shell.execute_reply":"2022-07-12T13:27:14.311983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir data","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:14.844084Z","iopub.execute_input":"2022-07-12T13:27:14.84447Z","iopub.status.idle":"2022-07-12T13:27:15.615102Z","shell.execute_reply.started":"2022-07-12T13:27:14.844436Z","shell.execute_reply":"2022-07-12T13:27:15.613985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r '../input/mayo-clinic-strip-ai/other.csv' './data'\n!cp -r '../input/mayo-clinic-strip-ai/sample_submission.csv' './data'\n!cp -r '../input/mayo-clinic-strip-ai/test.csv' './data'\n!cp -r '../input/mayo-clinic-strip-ai/train.csv' './data'","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:16.752299Z","iopub.execute_input":"2022-07-12T13:27:16.752696Z","iopub.status.idle":"2022-07-12T13:27:19.807836Z","shell.execute_reply.started":"2022-07-12T13:27:16.752663Z","shell.execute_reply":"2022-07-12T13:27:19.806057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd data","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:19.810253Z","iopub.execute_input":"2022-07-12T13:27:19.810637Z","iopub.status.idle":"2022-07-12T13:27:19.818625Z","shell.execute_reply.started":"2022-07-12T13:27:19.810599Z","shell.execute_reply":"2022-07-12T13:27:19.817626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir train\n!mkdir test\n!mkdir other","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:20.336283Z","iopub.execute_input":"2022-07-12T13:27:20.337212Z","iopub.status.idle":"2022-07-12T13:27:22.622437Z","shell.execute_reply.started":"2022-07-12T13:27:20.33716Z","shell.execute_reply":"2022-07-12T13:27:22.62117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for filename in tqdm(os.listdir(OTHER_IMAGES_PATH), total=other_meta_data.shape[0]):\n    img_path = OTHER_IMAGES_PATH+'/'+filename\n    if os.stat(img_path).st_size < 1.5e+9: # file size less than 1.5 GiB\n        img = load_image(img_path)\n        img.save(f'./other/{filename[:-3]}jpg')\n        del img\n        gc.collect()\n    else:\n        print('FAILED', img_path)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T13:27:22.624608Z","iopub.execute_input":"2022-07-12T13:27:22.625256Z","iopub.status.idle":"2022-07-12T15:06:59.502789Z","shell.execute_reply.started":"2022-07-12T13:27:22.625216Z","shell.execute_reply":"2022-07-12T15:06:59.499364Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for filename in tqdm(os.listdir(TEST_IMAGES_PATH), total=test_meta_data.shape[0]):\n    img_path = TEST_IMAGES_PATH+'/'+filename\n    if os.stat(img_path).st_size < 1.5e+9: # file size less than 1.5 GiB\n        img = load_image(img_path)\n        img.save(f'./test/{filename[:-3]}jpg')\n        del img\n        gc.collect()\n    else:\n        print(img_path)\n        print('FAILED', img_path)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T15:08:29.021752Z","iopub.execute_input":"2022-07-12T15:08:29.022076Z","iopub.status.idle":"2022-07-12T15:09:33.950698Z","shell.execute_reply.started":"2022-07-12T15:08:29.022046Z","shell.execute_reply":"2022-07-12T15:09:33.949503Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for filename in tqdm(os.listdir(TRAIN_IMAGES_PATH), total=train_meta_data.shape[0]):\n    img_path = TRAIN_IMAGES_PATH+'/'+filename\n    if os.stat(img_path).st_size < 1.5e+9: # file size less than 1.5 GiB\n        img = load_image(img_path)\n        img.save(f'./train/{filename[:-3]}jpg')\n        del img\n        gc.collect()\n    else:\n        print(img_path)\n        print('FAILED', img_path)","metadata":{"execution":{"iopub.status.busy":"2022-07-12T15:09:33.952692Z","iopub.execute_input":"2022-07-12T15:09:33.953152Z","iopub.status.idle":"2022-07-12T16:50:36.651832Z","shell.execute_reply.started":"2022-07-12T15:09:33.95311Z","shell.execute_reply":"2022-07-12T16:50:36.649727Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}