Python 3 OpenCV Script to Crop Image into Different Shapes Using Numpy and Save Images in a Folder
722 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-opencv-script-to-crop-image-into-different-shapes-using-numpy-and-save-images-in-a-folder/
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a Python
0:04
script which uses Open CV and numpy
0:07
package to crop images into different
0:09
shapes such as circular triangle and
0:13
ellipse so let me take a very simple
0:16
example so we take this input image
0:19
which is present in my working directory
0:21
so we have this simple image and if I
0:24
apply this Python script so what will
0:26
happen it will actually create output
0:29
directory and
0:32
then you'll see
0:36
output/shapes so it will crop this image
0:38
into circular
0:40
ellipse this is a circular one this is
0:43
ellipse this is the rectangle crop and
0:46
triangle crop so it will crop this image
0:48
into multiple different shapes using
0:51
this Python script you can modify the
0:55
script to actually match the coordinates
0:57
as well so different images will yield
0:59
different results so once again if I
1:02
take a different image for
1:08
example so again it will actually crop
1:11
as you can
1:16
see it will crop this image into
1:18
multiple shapes so this is a script i've
1:21
given the script in the description of
1:23
the video so first of all guys you need
1:25
to install some modules for this
1:29
so just go to this website and just
1:33
install first of all numpy package which
1:36
is actually a fundamental package inside
1:39
Python for computing then we also need
1:42
to install uh the package which will be
1:46
I think yeah Open
1:50
CV which is again a very popular package
1:53
inside Python so install this as well so
1:55
after that we simply create the
2:03
script so let me just show you step by
2:06
step how the script is created
2:09
so first of all we just import all the
2:12
packages which are necessary which is
2:14
your open cv package the numpy
2:19
package and then the operating system
2:22
package so here we simply load the input
2:24
image by the open cv method image
2:31
read so yeah our images is present
2:35
inside after we load the input image we
2:38
simply specify the height and the width
2:43
so we use this image
2:46
shape then we specify the output folder
2:49
where all the images will be created
2:53
so then we specify or make these
2:57
directories by the operating system
2:59
module and then we apply these three
3:01
shapes so first shape will be the
3:04
circular mask so we'll mark this image
3:07
into a circular shape so this is
3:09
actually the code for this so we use the
3:12
numpy library we draw the circle using
3:15
open cv save the result as circles
3:19
crop.png
3:20
then we do the same thing for triangle
3:23
as well this will actually crop the
3:26
image into a triangle shape so this
3:30
computing is done by numpy library you
3:32
can see all these functions and then we
3:35
crop this into triangle
3:37
crop.png and lastly we do this for
3:40
rectangle rectangle
3:43
cropping so all these methods are there
3:45
inside numpy then we apply the rectangle
3:48
method of open CV and crop this image
3:51
into the form of rectangle and lastly we
3:55
do this for
3:56
ellipse which is again a different shape
3:59
again we call this method cv2 ellipse
4:03
and
4:04
numpy for
4:07
computing and lastly we draw this or
4:10
print out that all the shapes are
4:13
cropped and saved to this output folder
4:15
so once again if I run this script now
4:18
all the script is given in the
4:20
description of the video so it will
4:21
create a output shapes folder where all
4:24
the shapes will be
4:26
there so you can see that different
4:29
shapes will be
4:32
there so thank you very much guys for
4:34
watching this video and please visit on
4:37
my website freemediattools.com
4:39
uh which contains thousands of tools
#Programming
#Scripting Languages