Skip to content

Getting started

This page takes you from zero to a folder of tiles in about five minutes. You need Python 3.10 or newer.

1. Install

uv add plakakia
pip install plakakia
uv add "plakakia[hyperspectral,geo]"   # or: pip install "plakakia[hyperspectral,geo]"

OpenCV windows

plakakia depends on opencv-python-headless, which has no GUI. If you want cv2.imshow, also install opencv-python in the same environment.

2. Tile one image

Any NumPy array works: an image read with OpenCV, Pillow or anything else.

import cv2
from plakakia import tile_image

image = cv2.imread("aerial.jpg")  # shape (1500, 2400, 3)
tiles, coords = tile_image(
    image,
    tile_size=640,  # each tile is 640 x 640 pixels
    step_size=480,  # move 480 px between tiles, so neighbours overlap by 160 px
    cover_edges=True,  # add tiles at the right and bottom borders, so no pixel is lost
)

You get back two arrays:

Name Shape What it holds
tiles (15, 640, 640, 3) The tiles, stacked like a batch.
coords (15, 4) Where each tile came from: (x1, y1, x2, y2) in the image.

The input image with the 15 tiles drawn on it

The 15 tiles from the code above, drawn on the input image. Darker bands are where neighbouring tiles overlap.

tiles[i] is exactly image[y1:y2, x1:x2] for coords[i]. Loop over them to run a model, save them, or look at them:

for tile, (x1, y1, x2, y2) in zip(tiles, coords):
    cv2.imwrite(f"tile_{x1}_{y1}.jpg", tile)

What should tile_size and step_size be?

Use your model's input size as tile_size (640 for most YOLO models). Set step_size to 70 to 80% of it, so that an object cut by one tile border is whole in the neighbouring tile. Try it in the playground.

3. Bring the labels along

If the image has bounding boxes, plakakia tells you which boxes fall in which tile and converts them to tile coordinates:

from plakakia import assign_boxes_to_tiles, clip_boxes_to_tile, read_yolo

ann = read_yolo("aerial.txt", image_size=image.shape[:2])  # boxes in pixels
inside = assign_boxes_to_tiles(ann.boxes, coords, min_visibility=0.6)

for t, tile in enumerate(tiles):
    keep = inside[t]  # boxes in tile t
    boxes = clip_boxes_to_tile(ann.boxes[keep], coords[t])  # now relative to the tile
    labels = ann.labels[keep]

tile with boxes tile with boxes tile with boxes

min_visibility=0.6 keeps a car in a tile if at least 60% of it is inside. How tiling works shows what that means.

4. Tile a whole dataset

For a folder of images and labels, you do not need to write any Python. Describe the job in a YAML file:

config.yaml
input_dir_images: data/images
input_dir_annotations: data/labels
input_format_annotations: yolo   # yolo | pascal_voc | coco | segmentation | none
tile_size: 640
step_size: 480
cover_edges: true
num_workers: -1                  # use every CPU core

and run:

plakakia --config config.yaml

The tiles land in output/images and their labels in output/annotations, ready for training. See Offline tiling for every option.

5. Large files and streaming

tile_image copies the tiles into a new array. For very large images, ask for the window positions only and slice them yourself. Slices are views, so nothing is copied:

from plakakia import tile_coordinates

for x1, y1, x2, y2 in tile_coordinates(*image.shape[:2], tile_size=640, step_size=480):
    window = image[y1:y2, x1:x2]

For files larger than memory, see iter_tiles.

Try it without code

The interactive demo lets you upload an image and watch it being tiled:

uv run --extra demo streamlit run demo/app.py

Next steps