Crop the Image Intuitively — NumPy
In this blog article, we will learn how to crop an image in Python using NumPy as an ideal library. When we talk about images, they are just matrices in 2D space. And of course, it depends on the image, if it is an RGB image then the size of the image would be (width, height, 3) otherwise — grayscale would just be (width, height). But ultimately, images are just large matrices where each value is a pixel positioned row-wise and column-wise accordingly.
Cropping the image is just obtaining the sub-matrix of the image matrix. The size of the sub-matrix (cropped image) can be of our choice and mainly it is the height and width. There needs to be one important thing for the image to be cropped, i.e., starting position. The starting position is helpful for obtaining the sub-matrix from that position and depending upon height and width we can easily crop cut the image.
The three important things are:
- starting_position
- length (height)
- width
Based on these three things, we can construct our cropping function completely ready.
Time to Code
The packages that we mainly use are:
- NumPy
- Matplotlib
- OpenCV → It is only used for reading the image.

Import the Packages
Read the Image
The above function reads the image either in grayscale or RGB and returns the image matrix.
Cropping the Image
We need to pass the above mentioned 3 things as arguments in our function. But before doing let’s try to crop (slice) the matrix with NumPy.
>>> import numpy as np
>>> m = np.array([
... [1, 2, 3, 4, 5, 6, 7],
... [5, 3, 4, 2, 1, 7, 6],
... [6, 4, 3, 5, 1, 2, 7],
... [5, 6, 3, 1, 4, 2, 7],
... [1, 2, 3, 4, 5, 6, 7]
... ])
>>>
>>> print(m)
[[1 2 3 4 5 6 7]
[5 3 4 2 1 7 6]
[6 4 3 5 1 2 7]
[5 6 3 1 4 2 7]
[1 2 3 4 5 6 7]]
>>>
>>> crop_m = m[1:4, 2:7]
>>> print(crop_m)
[[4 2 1 7 6]
[3 5 1 2 7]
[3 1 4 2 7]]
>>>
The above code is an example of how we can crop an image matrix. Notice crop_m is the cropped matrix (sub-matrix) that is sliced from the original matrix m. The sub-matrix crop_m is taking values from [1:4, 2:7], i.e., values from 1st row till 4th row and from 2nd column till 7th column. We should something similar for the image to obtain the cropped image. Let’s write the cropping image function.
Let’s understand what this function will actually result in.
- At the first step, we read the image either in grayscale or RGB and obtain the image matrix.
- We obtain the height and width of the image which is further used in the validation of the code.
- We make sure that the length and width are positive integers. Hence absolute values are considered.
- We calculate the four important values which are useful for slicing the matrix — start_row, end_row, start_column, end_column. We obtain that using the three arguments that are passed — start_pos, length, width.
- We obtain the cropped image by slicing the matrix.
- We plot both the original and cropped images for the visualization.
Let’s test the above function —
For RGB Image
start row - 199
end row - 299
start column - 199
end column - 399

For Grayscale Image
start row - 199
end row - 299
start column - 199
end column - 399

This is it!!! We finally are able to crop the image by just knowing the starting position and length & width of the cropped image. Isn’t it great? We can also add a lot of customization options like adding a border around the image and other things. To know how to add a border to the image, you can refer to my article.
Other similar articles can be found in my profile. Have a great time reading and implementing the same.
If you liked it, you can buy coffee for me from here.
