We’re going to demonstrate using the OpenCV library in Python to draw different shapes on images. We’ll also show you how to add text to on an image using the same library. Show
OpenCV (Open Source Computer Vision) library is one of the most widely used programming libraries for computer vision and image processing tasks. OpenCV was originally written in C++, however numerous OpenCV wrappers have been written for other languages so you can execute OpenCV functions in all kinds of programming languages. Installing OpenCV Wrapper for PythonTo install the Python wrapper for OpenCV, execute the following pip command on your command terminal. pip install opencv-python Opening an Image with OpenCVThe following lines of code import the libraries required to execute scripts in this tutorial. We’ll import NumPy, Matplotlib, and OpenCV. Here, cv2
is an alias for the OpenCV wrapper for Python. The
If you don’t already have NumPy and Matplotlib installed, you’ll need to first install them using the following pip commands on your command terminal: pip install numpy pip install matplotlib To open an image using the OpenCV, you need to call the
Output: Like any other NumPy array, you can plot the opened image using the
Output: The colors look a bit weird in the car image above. This is because OpenCV reads an image in the Blue-Green-Red (BGR) format where blue, green and red are the three color channels for an image. On the other hand, Matplotlib expects an image to be in Red-Green-Blue (RGB) format. Therefore, Matplotlib swaps the red and blue channels, hence image colors look strange. You can correct image colors by converting the BGR format to RGB. To do so, simply use the
The output now shows the image in its original colors. Output: Creating a Blank ImageBefore we start drawing shapes, let’s see how to create a blank image with Python. You’ll be drawing shapes on this image. Since an image returned by the OpenCV The following script creates an image of size 768 x 768 with three dimensions. The image is drawn in the output.
Output: Drawing a LineNow that we have our blank canvas, let’s first see how to draw a line on an image. To draw a line, you need to call the Next, you need to pass two tuples to the You also need to pass the RGB values to the
Output: Drawing a RectangleTo draw a rectangle using OpenCV, use the The following script draws a magenta rectangle on the blank image.
Output: Notice we’re not creating new images each time we draw a new shape on our blank canvas. It’s important to remember that the methods for drawing shapes with
OpenCV are Drawing a CircleThe The following script draws a green circle that is centered at 500 pixels from the right and 500 pixels from the top of an image. The circle radius is 100 pixels.
Output: Get Our Python Developer Kit for Free I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way. Drawing a PolygonYou’re not limited to basic geometric shapes. You can also plot any custom polygon with any number of vertices using the Let’s use
Output: See how the
pol_vert numpy array is two-dimensional? You need to convert it into three dimensions to plot the shape with a color. To convert the pol_vert array to three-dimensions, you can use the
Output: The pol_vert array is now converted into a three-dimensional array so we can use it to draw our polygon. To do so, you need to pass the
image and the list of vertices to the
Output: Adding Text to an ImageOpenCV even lets you add text to an image. To do so, call the As an example, here’s a script that draws the text “OpenCV” in black letters on an image.
Output: Drawing a Shape on an Input ImageIn the previous sections, we drew shapes on a blank image, but you can draw shapes on any image you want. To demonstrate, the following script uses OpenCV to draw a rectangle on our image of the car from earlier.
Output: Get Our Python Developer Kit for Free I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way. This article was written by Usman Malik, contributing writer for The Python Tutorials Blog. Can we draw images using Python?The ImageDraw module provides simple 2D graphics for Image objects. You can use this module to create new images, annotate or retouch existing images, and to generate graphics on the fly for web use. For a more advanced drawing library for PIL, see the aggdraw module.
How do you sketch an image in Python?Convert Image to a Pencil Sketch. Step 1: Convert to Grey Image. Using cvtColor function of OpenCV. ... . Step 2: Invert Image. ... . Step 3: Blur image. ... . Step 4: Invert Blurred Image. ... . Step 5: Sketch. ... . Step 6: Save Sketch. ... . Step 7: Display sketch.. How do you write to an image in Python?The save() function writes an image to file. Like for reading (open() function), the save() function accepts a filename, a path object or a file object that has been opened to write.
How do you draw an object in Python?Object-oriented Programming in Python: Create Your Own Adventure Game. from shapes import Paper, Triangle, Rectangle, Oval.. paper = Paper(). rect1 = Rectangle(). rect1. set_width(200) rect1. set_height(100) rect1. ... . rect1. draw(). paper. display(). # put the code to create the 2nd Rectangle here paper. display(). |