text_plain_output_1.png
from glob import glob

import cv2

import numpy as np # linear algebra

import os

import os
from glob import glob
TRAIN_DATA = '../input/train'
type_1_files = glob(os.path.join(TRAIN_DATA, 'Type_1', '*.jpg'))
type_1_ids = np.array([s[len(os.path.join(TRAIN_DATA, 'Type_1')) + 1:-4] for s in type_1_files])
type_2_files = glob(os.path.join(TRAIN_DATA, 'Type_2', '*.jpg'))
type_2_ids = np.array([s[len(os.path.join(TRAIN_DATA, 'Type_2')) + 1:-4] for s in type_2_files])
type_3_files = glob(os.path.join(TRAIN_DATA, 'Type_3', '*.jpg'))
type_3_ids = np.array([s[len(os.path.join(TRAIN_DATA, 'Type_3')) + 1:-4] for s in type_3_files])

def get_filename(image_id, image_type):
    """
    Method to get image file path from its id and type   
    """
    try:
        ['Type_1', 'Type_2', 'Type_3'].index(image_type)
    except:
        raise Exception("Image type '%s' is not recognized" % image_type)
    ext = 'jpg'
    data_path = os.path.join(TRAIN_DATA, image_type)
    return os.path.join(data_path, '{}.{}'.format(image_id, ext))
import cv2
def get_image_data(image_id, image_type):
    """
    Method to get image data as np.array specifying image id and type
    """
    fname = get_filename(image_id, image_type)
    img = cv2.imread(fname)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return img

img = get_image_data('497', 'Type_1')

tile_size = (256, 256)
n = 10
m = int(np.floor(len(type_1_ids) / n))
complete_image = np.zeros((m * (tile_size[0] + 2), n * (tile_size[1] + 2), 3), dtype=np.uint8)
train_ids = sorted(type_1_ids)
counter = 0
for i in range(m):
    ys = i * (tile_size[1] + 2)
    ye = ys + tile_size[1]
    for j in range(n):
        xs = j * (tile_size[0] + 2)
        xe = xs + tile_size[0]
        image_id = train_ids[counter]
        counter += 1
        img = get_image_data(image_id, 'Type_1')
        img = cv2.resize(img, dsize=tile_size)
        complete_image[ys:ye, xs:xe] = img[:, :, :]
print(complete_image.shape)