Crop Image with OpenCV-Python Last Updated : 03 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Cropping an Image is one of the most basic image operations that we perform in our projects. In this article, we will discuss how to crop images using OpenCV in Python. Stepwise Implementation For this, we will take the image shown below. Step 1: Read the image cv2.imread() method loads an image from the specified file. If the image cannot be read (because of the missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix. Note: When we load an image in OpenCV using cv2.imread(), we store it as a Numpy n-dimensional array. Example: Python program to read the image Python3 import cv2 # Read Input Image img = cv2.imread("test.jpeg") # Check the type of read image print(type(img)) # Display the image cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() Output: Step 2: Get the image Dimensions We can see the type of 'img' as 'numpy.ndarray'. Now, we simply apply array slicing to our NumPy array and produce our cropped image, So we have to find the dimensions of the image. For this we will use the image.shape attribute. Syntax: image.shape where image is the input image Example: Python code to find the dimensions of the image, Python3 import cv2 # read the image img = cv2.imread("test.jpeg") print(type(img)) # Check the shape of the input image print("Shape of the image", img.shape) Output: image shapeStep 3: Slice the image Now we can apply array slicing to produce our final result. Syntax : image[rows,columns] where rows are the row slicecolumns is the column slice Example: Python3 import cv2 img = cv2.imread("test.jpeg") print(type(img)) # Shape of the image print("Shape of the image", img.shape) # [rows, columns] crop = img[50:180, 100:300] cv2.imshow('original', img) cv2.imshow('cropped', crop) cv2.waitKey(0) cv2.destroyAllWindows() Output: Comment More infoAdvertise with us Next Article Crop Image with OpenCV-Python A ayushmankumar7 Follow Improve Article Tags : Python OpenCV Python-OpenCV Practice Tags : python Similar Reads Python | Crop image using pillow In this article, we will learn to crop an image using pillow library. Cropping an image means to select a rectangular region inside an image and removing everything outside the rectangle. To crop an image we make use of crop() method on image objects. Syntax : IMG.crop(box_tuple) Parameters : Image_ 1 min read Python PIL | Image.crop() method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. PIL.Image.crop() method is used to crop a rectangular portion of any image. Syntax: PIL.Image.crop(box = None)Parameters: box - a 4-tuple defining the left, upper, right, and lower pixel coordin 2 min read Watermarking images with OpenCV and Python In this article, we are going to see how to make watermarking images using OpenCV in Python. Watermark is intentionally left Text/Logo onto the image. Watermarks are generally used by artists to protect the copyright of the image. Using watermarks we can ensure that the owner of the image is the per 4 min read Cropping Faces from Images using OpenCV - Python Opencv is a python library mainly used for image processing and computer vision. In this article first, we detect faces after that we crop the face from the image. Face detection is the branch of image processing that uses to detect faces. We will use a pre-trained Haar Cascade model to detect faces 3 min read How to install OpenCV Contrib Python OpenCV (Open Source Computer Vision Library) is a powerful open-source computer vision and machine learning software library. It provides various tools and algorithms for image and video processing, object detection, feature extraction, and more. OpenCV Contrib is an extension module that includes a 2 min read Like