Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

How to crop an image in Python

  • Safa MulaniSafa Mulani
  • April 7, 2020
  • Python Programming Examples
How to crop an image in Python

Hello! In this article, we will be focusing on different ways to crop an image in Python. Now, let us unveil and understand the background functions being used to crop an image.


Technique 1: Python PIL to crop an image

PIL stands for ‘Python Image Library‘. PIL adds image editing and formatting features to the python interpreter. Thus, it has many in-built functions for image manipulation and graphical analysis.

PIL has in-built Image.crop() function that crops a rectangular part of the image.

Syntax:

Image.crop(left, top, right, bottom)
  • top and left: These parameters represent the top left coordinates i.e (x,y) = (left, top).
  • bottom and right: These parameters represent the bottom right coordinates i.e. (x,y) = (right, bottom).

The area to be cropped is represented as follows:

  • left <= x < right
  • top <= y < bottom

Example:

from PIL import Image 

 
img = Image.open(r"C:\Users\HP\OneDrive\Desktop\<image>.png") 


left = 0
top = 50
right = 510
bottom = 292

 
img_res = img.crop((left, top, right, bottom)) 


img_res.show() 

In the above example, Image.open(r"image path") is a function of PIL to open an image in read mode.

We have assigned certain values to the left, right, top and bottom coordinates.

Image.show() function is used to display the cropped image.

Original Image:

Image Used For Cropping Purpose
Image Used For Cropping Purpose

Cropped Image (output):

Cropped Image Using PIL
Cropped Image Using PIL

Technique 2: Crop an Image in Python using OpenCV

Python OpenCV is a library with a large number of functions available for real-time computer vision. It contains a good set of functions to deal with image processing and manipulation of the same.

In order to process an image using OpenCV, the users need to install OpenCV library with a version of 3.0 and above.

At first, we need to import the OpenCV library in our program using the below code snippet:

import cv2

OpenCV actually performs slicing of the image passed as an array in the method of cropping an image.

Syntax:

image[start_x:end_x, start_y:end_y]
  • The image[] actually slices the image in the form of arrays by passing the start and end index of x and y coordinates.
  • Thus, the image between the start and end coordinates of x and y is returned as the cropped array object.

Example:

import cv2
image = cv2.imread(r"C:\Users\HP\OneDrive\Desktop\<image>.png")

y=0
x=0
h=300
w=510
crop_image = image[x:w, y:h]
cv2.imshow("Cropped", crop_image)
cv2.waitKey(0)

The cv2.imread(r"image path") function is used to open an image in read mode.

Further, the start and end indexes for the x and y-axis are provided and thus the image is cropped.

The cv2.imshow() function is used to display the cropped image. We’ve used the same image as before here.

Cropped Image (output):

Cropped Image Using OpenCV
Cropped Image Using OpenCV

Conclusion

Thus, in this article, we have understood the ways to crop an image in Python.


References

  • Cropping an image in Python using OpenCV — StackOverFlow
Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189
Using Python round()
Previous Post Using Python round()
Next Post Python format() function
Python format() function

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.