Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Letterboxing

Letterboxing resizes images to a target shape while preserving aspect ratio with padding, keeping objects undistorted and the full field of view.

Letterboxing is an image-resizing technique that preserves an image's aspect ratio while adding padding to reach a target size. It scales the image uniformly, then fills unused space with border pixels, keeping objects' proportions and the full field of view. In computer vision, it prepares differently shaped images for models that accept a common input shape.

Outside AI, letterboxing also describes displaying widescreen film with horizontal bars.

How Letterboxing Preserves Image Geometry#

Aspect ratio is the relationship between image width and height. During data preprocessing, letterboxing selects the smaller of two scale factors: target width divided by original width, and target height divided by original height. Both dimensions use that factor, so the resized image fits inside the target. Remaining pixels become padding. TensorFlow's resize-and-pad operation implements this approach with zero-valued borders.

For example, a 1280 x 720 image fitted into a 640 x 640 target uses a scale factor of 0.5. Its content becomes 640 x 360, leaving 280 rows of padding: 140 above and 140 below when centered. Portrait images receive side borders. A letterboxed image can therefore have horizontal or vertical padding; the target can also be rectangular.

Ultralytics YOLO uses the LetterBox transform to preserve geometry for detection, segmentation, and pose inputs, with gray padding valued at 114 by default. For YOLO26 detection, segmentation, and pose prediction, minimum-rectangle padding reduces borders to a compatible stride multiple when batch shapes and the inference backend support it.

Letterboxing vs. Stretching and Cropping#

Three resizing choices handle mismatched image and target shapes differently:

  • Letterboxing preserves proportions and the full image, allocating part of the input to padding.
  • Stretching resizes width and height independently to fill the target. Circles become ellipses when the scale factors differ.
  • Cropping preserves proportions when paired with uniform resizing, then removes content outside the selected region. Objects near image edges can be cut off.

These choices affect object detection, where shapes and positions matter, and image classification, where cropping changes the visible evidence for a class.

Padding also consumes input area. In the 1280 x 720 example, image content occupies 56.25% of the square and padding occupies 43.75%. Downsampling reduces fine detail inside that content; adding border pixels creates no additional object detail. For high-resolution images containing small objects, tiled inference processes separate image regions at a larger effective object scale.

A Python Letterboxing Example#

Install Pillow with pip install pillow. Its ImageOps.pad operation resizes and centers an image on a padded canvas. This example uses the bus photograph distributed for vision tutorials.

from urllib.request import urlretrieve

from PIL import Image, ImageOps

urlretrieve("https://ultralytics.com/images/bus.jpg", "bus.jpg")
image = Image.open("bus.jpg")
image = image.convert("RGB")

# Preserve proportions and fill the unused area with gray.
letterboxed = ImageOps.pad(image, (640, 640), color=(114, 114, 114))
letterboxed.save("letterboxed_bus.png")
print(f"{image.size} -> {letterboxed.size}")

The output dimensions are (810, 1080) -> (640, 640), expressed as width and height. The saved image contains the complete photograph resized to 480 x 640, with an 80-pixel border on each side.

Mapping Annotations and Predictions#

Letterboxing transforms the coordinate system as well as the pixels. Every bounding box coordinate must be scaled and shifted by the left or top padding. The same geometry applies to keypoints in pose estimation and object outlines in instance segmentation. Leaving annotations unchanged during training misaligns targets with the objects they describe.

After inference, coordinate rescaling reverses the transformation: subtract the padding offset, then divide by the resize scale. For an otherwise correctly positioned box with nonzero padding, omitting that offset shifts the box and lowers its intersection over union with the ground truth. In the bus example, a point at padded coordinates (320, 320) maps to original coordinates (405, 540) after subtracting 80 from x and dividing both coordinates by 640 / 1080.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning