{"cells":[{"metadata":{},"cell_type":"markdown","source":"This notebook shows you how to resize your image into a smaller format (e.g. 128px, 256px) and perform various transforms to obtain a specific shape (e.g. pad or stretch to square). It also saves the data into a `tar.gz` file that you can use to create a dataset.\n\nFor the list of datasets created from (variations of) this notebook, check out [this discussion post](https://www.kaggle.com/c/bms-molecular-translation/discussion/223477). You will be able to find various shapes, sizes and file format (png and jpg).\n\n\n\nBy using the output of this notebook, you are accepting the [competition rules](https://www.kaggle.com/c/bms-molecular-translation/rules)."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nfrom multiprocessing import Pool\n\nfrom PIL import Image, ImageOps\nimport pandas as pd\nfrom tqdm.auto import tqdm\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def convert_to_path(split: str):\n    # Modified from: https://www.kaggle.com/ihelon/molecular-translation-exploratory-data-analysis\n    def aux(image_id: str) -> str:\n        return \"../input/bms-molecular-translation/{}/{}/{}/{}/{}.png\".format(\n            split, image_id[0], image_id[1], image_id[2], image_id \n        )\n\n    return aux","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def pillow_pad(im, desired_size, color=0, resample=Image.LANCZOS, copy=False):\n    # Source: https://jdhao.github.io/2017/11/06/resize-image-to-square-with-padding/\n    \n    if copy:\n        im = im.copy()\n    \n    old_size = im.size  # old_size[0] is in (width, height) format\n\n    ratio = float(desired_size)/max(old_size)\n    new_size = tuple([int(x*ratio) for x in old_size])\n    # use thumbnail() or resize() method to resize the input image\n\n    # thumbnail is a in-place operation\n    im.thumbnail(new_size, resample)\n\n    # create a new image and paste the resized on it\n\n    new_im = Image.new(\"RGB\", (desired_size, desired_size), color=color)\n    new_im.paste(im, ((desired_size-new_size[0])//2,\n                        (desired_size-new_size[1])//2))\n\n    return new_im","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def resize(im, size, mode='keep_ratio', color=0, resample=Image.LANCZOS, inplace=True):\n    # Source: https://www.kaggle.com/xhlulu/bms-molecular-resizing-and-reshaping\n    if not inplace:\n        im = im.copy()\n    if mode == 'keep_ratio':\n        im.thumbnail((size, size), resample)\n    elif mode == \"pad\":\n        im = pillow_pad(im, desired_size=size, color=color, resample=resample, copy=False)\n    elif mode == 'stretch':\n        im = im.resize((size, size), resample)\n    else:\n        raise ValueError(\"Invalid mode. Please choose 'keep_ratio', 'pad', or 'stretch'.\")\n    \n    return im","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\ntrain = pd.read_csv('../input/bms-molecular-translation/train_labels.csv')\ntest = pd.read_csv('../input/bms-molecular-translation/sample_submission.csv')\n\ntrain_paths = train.image_id.apply(convert_to_path('train'))\ntest_paths = test.image_id.apply(convert_to_path('test'))\n\npaths = {\"train\": train_paths.tolist(), \"test\": test_paths.tolist()}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\nfor split in ['train', 'test']:\n    save_dir = f'/kaggle/tmp/{split}/'\n    os.makedirs(save_dir, exist_ok=True)\n    \n    def process_fn(path):\n        name = path.split('/')[-1]\n        im = Image.open(path)\n        im = resize(im, 128, mode='pad', color=(255, 255, 255))\n        im.save(save_dir + name.replace(\"png\", \"jpg\"))\n    \n    with Pool() as p:\n        p.map(process_fn, paths[split])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n!tar -zcf data.tar.gz -C \"/kaggle/tmp/\" .\n# alternatively:\n# !tar -cf data.tar -C \"/kaggle/tmp/\" .","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}