{"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":"code","source":"import os\nimport cv2\n\ndef generate_tiles(image_path, save_dir, tile_size):\n    \"\"\"\n    Function to generate tiles from a png whole slide image and save them in a specified directory.\n\n    Parameters:\n    image_path (str): Path to the input image file.\n    save_dir (str): Directory to save the generated tiles.\n    tile_size (int): Size of each square tile in pixels.\n    \"\"\"\n\n    # Read the input image using OpenCV\n    img = cv2.imread(image_path)\n\n    # Extract image dimensions (height, width, channels)\n    height, width, _ = img.shape\n\n    # Calculate the number of tiles in rows and columns\n    rows = height // tile_size\n    cols = width // tile_size\n\n    # Create the directory to save tiles if it doesn't exist\n    os.makedirs(save_dir, exist_ok=True)\n\n    # Iterate through rows and columns to generate tiles\n    for row in range(rows):\n        for col in range(cols):\n            # Calculate coordinates for each tile\n            x = col * tile_size\n            y = row * tile_size\n\n            # Extract the tile from the image\n            tile = img[y:y+tile_size, x:x+tile_size]\n\n            # Define the name and path for the tile\n            tile_name = f\"tile_{col}_{row}.png\"\n            tile_path = os.path.join(save_dir, tile_name)\n\n            # Save the tile as a PNG file\n            cv2.imwrite(tile_path, tile)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T12:57:17.764697Z","iopub.execute_input":"2023-11-24T12:57:17.765154Z","iopub.status.idle":"2023-11-24T12:57:17.776072Z","shell.execute_reply.started":"2023-11-24T12:57:17.76512Z","shell.execute_reply":"2023-11-24T12:57:17.774535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example usage\nimage_path = \"/kaggle/input/UBC-OCEAN/train_images/13568.png\"\nsave_dir = \"/kaggle/working/output_tiles/\"  # Directory to save the tiles\ngenerate_tiles(image_path, save_dir, tile_size=256)","metadata":{"execution":{"iopub.status.busy":"2023-11-24T12:48:04.196739Z","iopub.execute_input":"2023-11-24T12:48:04.197252Z","iopub.status.idle":"2023-11-24T12:48:05.299636Z","shell.execute_reply.started":"2023-11-24T12:48:04.197213Z","shell.execute_reply":"2023-11-24T12:48:05.298614Z"},"trusted":true},"execution_count":null,"outputs":[]}]}