How to crop an image using PIL?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Cropping an image with Pillow is simple once you understand the crop box format. The real mistakes happen around coordinate interpretation, bounds checking, and saving the result correctly. If those three things are handled, cropping becomes a straightforward operation you can reuse in scripts and pipelines.
Use Image.crop with a Four-Value Box
In Pillow, the crop box is:
(left, upper, right, lower)
The right and lower bounds are exclusive, so the output width is right - left and the output height is lower - upper.
That tuple is measured from the top-left corner of the image.
Check the Image Size Before Cropping
Hard-coding crop coordinates is fine only if you know the image dimensions in advance. In general, inspect the size first.
Knowing the size lets you validate that your crop box is sensible before you apply it.
Build a Safe Crop Helper
If coordinates may come from user input or another system, clip them to the image bounds.
This prevents negative coordinates or inverted boxes from silently producing bad results.
Center Crop Is a Useful Reusable Pattern
For avatars, thumbnails, and cards, a center crop is often what you want.
That gives a predictable crop without needing manual coordinates for every file.
Normalize Orientation First
Phone photos often contain EXIF orientation metadata. If you crop before normalizing orientation, the crop region may be applied to the wrong visual orientation.
This is a small step, but it avoids a lot of confusing "my crop is in the wrong place" bugs.
Batch Cropping Follows the Same Rules
Once the crop logic is correct, applying it across many images is just normal file iteration.
Keep output naming stable and avoid repeatedly re-saving the same JPEG in a loop if you care about quality.
Common Pitfalls
- Forgetting that the crop box is
(left, upper, right, lower). - Treating the right and lower edges as inclusive instead of exclusive.
- Applying crop coordinates without checking the image size first.
- Cropping phone images before handling EXIF orientation.
- Recompressing JPEGs repeatedly and losing quality unnecessarily.
Summary
- Use
image.crop((left, upper, right, lower))in Pillow. - Right and lower bounds are exclusive.
- Validate and clip crop coordinates when they are not fully trusted.
- Normalize EXIF orientation before cropping camera images.
- Reuse helpers such as center-crop and safe-crop for consistent results.
Related reading
- How to crop UIImage on oval shape or circle shape?
- How to debug Tensorflow segmentation fault in model.fit?
- How to detect how far the object on photo is from another objects on that photo?
- How to detect rotated object from image using OpenCV?
- How to Customize the time format for Python logging?
- How to deal with SettingWithCopyWarning in Pandas
- How to do multi-class image classification in keras?
- How to extract and recognize the vehicle plate number with Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.