LongestMaxSize

Targets:
image
mask
bboxes
keypoints
volume
mask3d
Image Types:uint8, float32

Rescale an image so that the longest side is equal to max_size or sides meet max_size_hw constraints, keeping the aspect ratio.

Arguments
max_size
int | Sequence | None

Maximum size of the longest side after the transformation. When using a list or tuple, the max size will be randomly selected from the values provided. Default: None.

max_size_hw
tuple[int | None, int | None] | None

Maximum (height, width) constraints. Supports:

  • (height, width): Both dimensions must fit within these bounds
  • (height, None): Only height is constrained, width scales proportionally
  • (None, width): Only width is constrained, height scales proportionally If specified, max_size must be None. Default: None.
interpolation
0 | 6 | 1 | 2 | 3 | 4 | 5
1

interpolation method. Default: cv2.INTER_LINEAR.

mask_interpolation
0 | 6 | 1 | 2 | 3 | 4 | 5
0

flag that is used to specify the interpolation algorithm for mask. Should be one of: cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4. Default: cv2.INTER_NEAREST.

area_for_downscale
image | image_mask |

Controls automatic use of INTER_AREA interpolation for downscaling. Options:

  • None: No automatic interpolation selection, always use the specified interpolation method
  • "image": Use INTER_AREA when downscaling images, retain specified interpolation for upscaling and masks
  • "image_mask": Use INTER_AREA when downscaling both images and masks Default: None.
p
float
1

probability of applying the transform. Default: 1.

Examples
>>> import albumentations as A
>>> import cv2
>>> # Using max_size
>>> transform1 = A.LongestMaxSize(max_size=1024, area_for_downscale="image")
>>> # Input image (1500, 800) -> Output (1024, 546)
>>>
>>> # Using max_size_hw with both dimensions
>>> transform2 = A.LongestMaxSize(max_size_hw=(800, 1024), area_for_downscale="image_mask")
>>> # Input (1500, 800) -> Output (800, 427)
>>> # Input (800, 1500) -> Output (546, 1024)
>>>
>>> # Using max_size_hw with only height
>>> transform3 = A.LongestMaxSize(max_size_hw=(800, None))
>>> # Input (1500, 800) -> Output (800, 427)
>>>
>>> # Common use case with padding
>>> transform4 = A.Compose([
...     A.LongestMaxSize(max_size=1024, area_for_downscale="image"),
...     A.PadIfNeeded(min_height=1024, min_width=1024),
... ])
Notes
  • This transform scales images based on their longest side:
    • If the longest side is smaller than max_size: the image will be upscaled (scale > 1.0)
    • If the longest side is equal to max_size: the image will not be resized (scale = 1.0)
    • If the longest side is larger than max_size: the image will be downscaled (scale < 1.0)
  • This transform will not crop the image. The resulting image may be smaller than specified in both dimensions.
  • For non-square images, both sides will be scaled proportionally to maintain the aspect ratio.
  • Bounding boxes and keypoints are scaled accordingly.
  • When area_for_downscale is set, INTER_AREA will be used for downscaling, providing better quality.