Pad
Targets:
image
mask
bboxes
keypoints
volume
mask3d
Image Types:uint8, float32
Pad the sides of an image by specified number of pixels.
Arguments
paddingint | tuple[int, int] | tuple[int, int, int, int]
0
Padding values. Can be:
- int - pad all sides by this value
- tuple[int, int] - (pad_x, pad_y) to pad left/right by pad_x and top/bottom by pad_y
- tuple[int, int, int, int] - (left, top, right, bottom) specific padding per side
filltuple[float, ...] | float
0
Padding value if border_mode is cv2.BORDER_CONSTANT
fill_masktuple[float, ...] | float
0
Padding value for mask if border_mode is cv2.BORDER_CONSTANT
border_mode0 | 1 | 2 | 3 | 4
0
OpenCV border mode
pfloat
1
probability of applying the transform. Default: 1.0.
Examples
>>> import numpy as np
>>> import albumentations as A
>>> import cv2
>>>
>>> # Prepare sample data
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
>>> bboxes = np.array([[10, 10, 50, 50], [40, 40, 80, 80]], dtype=np.float32)
>>> bbox_labels = [1, 2]
>>> keypoints = np.array([[20, 30], [60, 70]], dtype=np.float32)
>>> keypoint_labels = [0, 1]
>>>
>>> # Example 1: Pad all sides by the same value
>>> transform = A.Compose([
... A.Pad(padding=20, border_mode=cv2.BORDER_CONSTANT, fill=0),
... ], bbox_params=A.BboxParams(coord_format='pascal_voc', label_fields=['bbox_labels']),
... keypoint_params=A.KeypointParams(coord_format='xy', label_fields=['keypoint_labels']))
>>>
>>> # Apply the transform
>>> padded = transform(
... image=image,
... mask=mask,
... bboxes=bboxes,
... bbox_labels=bbox_labels,
... keypoints=keypoints,
... keypoint_labels=keypoint_labels
... )
>>>
>>> # Get the padded data
>>> padded_image = padded['image'] # Shape will be (140, 140, 3)
>>> padded_mask = padded['mask'] # Shape will be (140, 140)
>>> padded_bboxes = padded['bboxes'] # Bounding boxes coordinates adjusted to the padded image
>>> padded_keypoints = padded['keypoints'] # Keypoints coordinates adjusted to the padded image
>>>
>>> # Example 2: Different padding for sides using (pad_x, pad_y)
>>> transform_xy = A.Compose([
... A.Pad(
... padding=(10, 30), # 10px padding on left/right, 30px on top/bottom
... border_mode=cv2.BORDER_CONSTANT,
... fill=128 # Gray padding color
... ),
... ])
>>>
>>> padded_xy = transform_xy(image=image)
>>> padded_xy_image = padded_xy['image'] # Shape will be (160, 120, 3)
>>>
>>> # Example 3: Different padding for each side
>>> transform_sides = A.Compose([
... A.Pad(
... padding=(5, 10, 15, 20), # (left, top, right, bottom)
... border_mode=cv2.BORDER_CONSTANT,
... fill=0,
... fill_mask=0
... ),
... ], bbox_params=A.BboxParams(coord_format='pascal_voc', label_fields=['bbox_labels']))
>>>
>>> padded_sides = transform_sides(
... image=image,
... mask=mask,
... bboxes=bboxes,
... bbox_labels=bbox_labels
... )
>>>
>>> padded_sides_image = padded_sides['image'] # Shape will be (130, 120, 3)
>>> padded_sides_bboxes = padded_sides['bboxes'] # Bounding boxes adjusted to the new coordinates
>>>
>>> # Example 4: Using different border_mode options
>>> # Create a smaller image for better visualization of reflection/wrapping
>>> small_image = np.random.randint(0, 256, (10, 10, 3), dtype=np.uint8)
>>>
>>> # Reflection padding
>>> reflect_pad = A.Compose([
... A.Pad(padding=5, border_mode=cv2.BORDER_REFLECT_101),
... ])
>>> reflected = reflect_pad(image=small_image)
>>> reflected_image = reflected['image'] # Shape will be (20, 20, 3) with reflected edges
>>>
>>> # Replicate padding
>>> replicate_pad = A.Compose([
... A.Pad(padding=5, border_mode=cv2.BORDER_REPLICATE),
... ])
>>> replicated = replicate_pad(image=small_image)
>>> replicated_image = replicated['image'] # Shape will be (20, 20, 3) with replicated edges
>>>
>>> # Example 5: Padding with masks and constant border mode
>>> binary_mask = np.zeros((50, 50), dtype=np.uint8)
>>> binary_mask[10:40, 10:40] = 1 # Set center region to 1
>>>
>>> mask_transform = A.Compose([
... A.Pad(
... padding=10,
... border_mode=cv2.BORDER_CONSTANT,
... fill=0, # Black padding for image
... fill_mask=0 # Use 0 for mask padding (background)
... ),
... ])
>>>
>>> padded_mask_result = mask_transform(image=image, mask=binary_mask)
>>> padded_binary_mask = padded_mask_result['mask'] # Shape will be (70, 70)References