{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Align image sizes without changing the ratio of different image sizes.\nバラバラな画像のサイズを比率をそのままで画像サイズを揃える","metadata":{}},{"cell_type":"code","source":"import os\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport os\n%matplotlib inline\nfrom PIL import Image\nimport cv2\nImage.MAX_IMAGE_PIXELS = 100000000000","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:34:53.504205Z","iopub.execute_input":"2023-12-01T11:34:53.504695Z","iopub.status.idle":"2023-12-01T11:34:54.229988Z","shell.execute_reply.started":"2023-12-01T11:34:53.504646Z","shell.execute_reply":"2023-12-01T11:34:54.228578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Adjust_image_size():\n    '''\n    Image Size Adjustment.\n    Fill in the margins with black.\n    \n    '''\n    def __init__(self, target_size, height_max, width_max):\n        self.target_size = target_size\n        if height_max > width_max:\n            self.max_size = height_max\n            self.max_direction = 'heigth'\n        else:\n            self.max_size = width_max\n            self.max_direction = 'width'\n            \n    def __call__(self, image:Image.Image):\n        width, height = image.size\n        if self.max_direction == 'width':\n            s = self.target_size / width\n            target_height = int(s*height)\n            image = image.resize((self.target_size, target_height))\n            img = Image.new(mode='RGB', size=(self.target_size, self.target_size), color=(0,0,0))\n        elif self.max_direction == 'heigth':\n            s = self.target_size / height\n            target_width = int(s*width)\n            image = image.resize((target_width, self.target_size))\n            img = Image.new(mode='RGB', size=(self.target_size, self.target_size), color=(0,0,0))\n        img.paste(im=image)\n        return img","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-01T11:34:54.232995Z","iopub.execute_input":"2023-12-01T11:34:54.233785Z","iopub.status.idle":"2023-12-01T11:34:54.248304Z","shell.execute_reply.started":"2023-12-01T11:34:54.233728Z","shell.execute_reply":"2023-12-01T11:34:54.246604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/input/UBC-OCEAN/train.csv'\ntrain = pd.read_csv(train_path)\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:34:54.250598Z","iopub.execute_input":"2023-12-01T11:34:54.251249Z","iopub.status.idle":"2023-12-01T11:34:54.307422Z","shell.execute_reply.started":"2023-12-01T11:34:54.251198Z","shell.execute_reply":"2023-12-01T11:34:54.305978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_des = train.describe() \nprint(train_des)\n\nwidth_max = int(train_des['image_width'].max())\nheight_max = int(train_des['image_height'].max())\nprint('width_max : ', width_max)\nprint('height_max : ', height_max)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:34:54.310747Z","iopub.execute_input":"2023-12-01T11:34:54.311826Z","iopub.status.idle":"2023-12-01T11:34:54.340881Z","shell.execute_reply.started":"2023-12-01T11:34:54.311769Z","shell.execute_reply":"2023-12-01T11:34:54.339526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"adjust = Adjust_image_size(512, height_max, width_max)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:34:54.342474Z","iopub.execute_input":"2023-12-01T11:34:54.343254Z","iopub.status.idle":"2023-12-01T11:34:54.349846Z","shell.execute_reply.started":"2023-12-01T11:34:54.343207Z","shell.execute_reply":"2023-12-01T11:34:54.348278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = '/kaggle/input/UBC-OCEAN/train_images/10077.png'\n\nimg = Image.open(filepath)\nprint(img.size)\nplt.axis(False)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:34:54.351986Z","iopub.execute_input":"2023-12-01T11:34:54.352513Z","iopub.status.idle":"2023-12-01T11:41:33.929572Z","shell.execute_reply.started":"2023-12-01T11:34:54.352446Z","shell.execute_reply":"2023-12-01T11:41:33.928363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = adjust(img)\nprint(img.size)\nplt.axis(False)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T11:41:33.931014Z","iopub.execute_input":"2023-12-01T11:41:33.931734Z","iopub.status.idle":"2023-12-01T11:41:50.482298Z","shell.execute_reply.started":"2023-12-01T11:41:33.931696Z","shell.execute_reply":"2023-12-01T11:41:50.480997Z"},"trusted":true},"execution_count":null,"outputs":[]}]}