How to crop an image in OpenCV using Python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Cropping in OpenCV is simpler than many beginners expect because you usually do not call a special crop function. In Python, an OpenCV image is a NumPy array, so cropping is done with array slicing, and the only thing you really need to remember is that the order is rows first and columns second.
Crop With NumPy Slicing
Once an image is loaded with OpenCV, you can select a rectangular region with standard slice syntax.
The expression image[100:300, 200:500] means:
- rows from 100 up to but not including 300
- columns from 200 up to but not including 500
That corresponds to y1:y2, x1:x2, not x1:x2, y1:y2. Mixing those up is the most common mistake.
Work With x, y, width, and height
Bounding boxes are often expressed as x, y, width, and height rather than explicit end coordinates. Convert them carefully before slicing.
This format is common when boxes come from object detectors, annotation tools, or mouse selections.
Add Bounds Checking for Real Data
Hard-coded crop coordinates are fine for one known image size, but production code should protect itself against out-of-range coordinates.
Without these checks, a bad annotation file or a resized source image can silently produce empty outputs.
Center Crop and Batch Cropping
Two common patterns are center cropping and extracting many boxes from one image.
A center crop is useful when you need a consistent region for previews or model input:
For multiple bounding boxes, loop over them and save or process each crop:
That pattern is common in preprocessing pipelines for machine learning datasets.
Displaying the Crop Correctly
If you show the cropped image with OpenCV, you can display it directly. If you use Matplotlib, convert from BGR to RGB first because OpenCV loads color images in BGR order.
If you skip the conversion, the crop still exists, but the colors will look wrong in Matplotlib.
Common Pitfalls
The first pitfall is mixing up x and y with row and column order. OpenCV images are indexed as image[y, x], so the first slice is vertical position and the second is horizontal position.
Another issue is assuming all images have the same size. Coordinates from one image may be invalid for another unless you clamp them or scale them after resizing.
Developers also sometimes save empty crops without checking dimensions. If the selected region falls outside the image, the result can be unusable while the script continues quietly.
Finally, remember the difference between display libraries. OpenCV and Matplotlib do not use the same default color order.
Summary
- Crop an OpenCV image in Python with NumPy slicing.
- Use
image[y1:y2, x1:x2], not the other way around. - Convert
x,y,width, andheightinto slice endpoints carefully. - Add bounds checking when coordinates come from external data.
- Convert BGR to RGB before showing cropped images with Matplotlib.

