{"cells":[{"metadata":{"_uuid":"999146e4498bb28bdb01e3b1bdc38af269708155"},"cell_type":"markdown","source":"## Introduction"},{"metadata":{"_cell_guid":"32718105-e9d1-4e94-ac4e-344628fcd46d","_uuid":"d2190150506b2a22bd6aaee2f6c917429d2b0b4a"},"cell_type":"markdown","source":"Following code reads in all images from the train_jpg and test_jpg folder. <br>\nAll the images provided are not of the same dimension. Thus, the code decides the maximum dimension and uniformly pads each image with zeros. These images are further concatenated into a single array of dimension (n_samples, x_max , y_max , 3) <br>\nn_samples  : # of images in the dataset <br>\nx_max         : Height of each padded image <br>\ny_max         :  Width of each padded image <br>\n###### *Currently the code is concatenating only 10 images*"},{"metadata":{"_cell_guid":"20082864-7df4-4545-b6cb-7d6e96878913","_uuid":"803998ddb7ee09af3629a0d932241296138da6ac"},"cell_type":"markdown","source":"## Loading Libraries"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport h5py\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport glob\nimport zipfile\n\n# Input data files are available in the \"../input/\" directory.\n\nimport os\nprint(os.listdir(\"../input\"))","execution_count":1,"outputs":[]},{"metadata":{"_cell_guid":"f6c3fa2a-33f5-4e35-a34e-1ef71bd3393e","_uuid":"26783aa1a90c6fb4f944c9a137b07668d0549154"},"cell_type":"markdown","source":"## Padding images and concatenate them to numpy array"},{"metadata":{"_cell_guid":"a903a7b2-4a50-453d-a3f5-11be1d5dac89","_uuid":"0bd5bc252fb8b720777cbc2aef10c5ee72d9dce3"},"cell_type":"markdown","source":"##### Function to get the maximum dimensions of the input data location"},{"metadata":{"_cell_guid":"d0cecca0-d9a3-4aed-8329-e47eaaa8c1b6","_uuid":"8a0c76502101670cd04178071aa98cab7152b393","collapsed":true,"trusted":true},"cell_type":"code","source":"def get_maximum_dimensions(archive):\n    # Get list of images from the directory\n    image_paths = archive.namelist()[0:10]\n    image_paths = filter(lambda x: '.jpg' in x, image_paths)\n    # Set the maximum dimension to which each image needs to be padded\n    data = []\n    [data.append(np.array(Image.open(archive.extract(i)).convert('RGB'))) for i in image_paths]\n    data = np.array(data)\n    dimensions = []\n    [dimensions.append(i.shape) for i in data]\n    dimensions = pd.DataFrame(dimensions)\n    x_max = max(dimensions.iloc[:,0])\n    y_max = max(dimensions.iloc[:,1])\n    return(x_max,y_max,data, image_paths)","execution_count":7,"outputs":[]},{"metadata":{"_cell_guid":"da7e4685-f352-4e1e-8c7e-777da3a2ee7f","_uuid":"808635fc7316fd8af2e36a1e4f579f56bbf4babd"},"cell_type":"markdown","source":"##### Function to get the padded images and a concatenated array for the data"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"trusted":true},"cell_type":"code","source":"def create_image_array(x_max,y_max,data):\n    # Zero pad images and obtain an array to work upon\n    data_final = []\n    for i in data:\n        left_pad = int((x_max - i.shape[0])/2)\n        right_pad = x_max - i.shape[0] - int((x_max - i.shape[0])/2)\n        top_pad = int((y_max - i.shape[1])/2)\n        bottom_pad = y_max - i.shape[1] - int((y_max - i.shape[1])/2)\n        data_final.append(np.pad(i , pad_width = ((left_pad,right_pad),(top_pad,bottom_pad),(0,0)),mode = 'constant',constant_values = 0))\n    data_final = np.array(data_final)\n    return(data_final)","execution_count":10,"outputs":[]},{"metadata":{"_cell_guid":"fd9964e6-e174-421f-aa61-0f695e0c6f22","_uuid":"058273f91fdefaed1fddbfd9f22c8e07e1f632a7","collapsed":true,"trusted":true},"cell_type":"code","source":"train_archive = zipfile.ZipFile('../input/train_jpg.zip', 'r')\ntest_archive = zipfile.ZipFile('../input/test_jpg.zip', 'r')","execution_count":4,"outputs":[]},{"metadata":{"_cell_guid":"eb4d2068-98bd-4aed-989c-84c2dbbe9c49","_uuid":"d822e80b82388f05e702cd88bac273552fae7552","collapsed":true,"trusted":true},"cell_type":"code","source":"## Get the dimensions of the images\ntrain_x_max, train_y_max, train_data, image_paths_train = get_maximum_dimensions(train_archive)\ntest_x_max, test_y_max, test_data, image_paths_train = get_maximum_dimensions(test_archive)\nx_max = max(train_x_max,test_x_max)\ny_max = max(train_y_max,test_y_max)","execution_count":11,"outputs":[]},{"metadata":{"_cell_guid":"a5b6607b-9e09-448e-9a3e-5940022d74d6","_uuid":"6a014a06790475e78549c7adb6a8b0402a9a3e44","collapsed":true,"trusted":true},"cell_type":"code","source":"## Pad the images and get create the numpy array\ntrain_images = create_image_array(x_max,y_max,train_data)\ntest_images = create_image_array(x_max,y_max,test_data)","execution_count":12,"outputs":[]},{"metadata":{"_cell_guid":"1cecd2bb-6c9f-4244-b42b-2ffacb7b64d1","_uuid":"1be87fd1303d8ef17e78b615ede1c856105ee1a8","trusted":true},"cell_type":"code","source":"## Dimensions of the images\nprint(train_images.shape)\nprint(test_images.shape)","execution_count":13,"outputs":[]},{"metadata":{"_cell_guid":"ab7e9088-6dee-4fc5-92ae-472ea19695ec","_uuid":"6f2a942c05bb45cc1023122251132b538dc722e6","trusted":true},"cell_type":"code","source":"## Plotting the train images to view the padded images\nplt.imshow(train_images[0,:,:,:])","execution_count":14,"outputs":[]},{"metadata":{"_cell_guid":"c23e1247-40a9-463e-bcf4-2c5a25c937c9","_uuid":"de5a644a8e9990e20117c5e45c2ad763d8c2d1ce","trusted":true},"cell_type":"code","source":"## Plotting the test images to view the padded images\nplt.imshow(test_images[0,:,:,:])","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}