Drawing lines on python

Python OpenCV: Drawing lines on image

In this tutorial we are going to learn how to draw lines in an image, using Python and OpenCV.

Introduction

In this tutorial we are going to learn how to draw lines in an image, using Python and OpenCV.

Being able to draw lines on an image might be useful to mark, for example, regions of interest on an image.

This tutorial was tested with version 4.0.0 of OpenCV and version 3.7.2 of Python.

The code

We will start our code by importing the cv2 module.

After that we are going to read an image, so we can later draw some lines on it. We can read the image with a call to the imread function, passing as input a string with the path of the image file in the file system.

image = cv2.imread('C:/Users/N/Desktop/test.jpg')

Next we are going obtain both the height and the width of the image, since we are going to use these values to draw one of the lines. You can check here a more detailed tutorial on how to obtain the dimensions of an image.

In short, we can obtain these dimensions by accessing the shape attribute of the image.

height = image.shape[0] width = image.shape[1]

Then, to draw a line, we need to use the line function of the cv2 module. This function receives as input the following parameters:

  • image: the image on which we want to draw the line.
  • point 1: first point of the line segment. This is specified as a tuple with the x and y coordinates.
  • point 2: second point of the line segment. This is specified as a tuple with the x and y coordinates.
  • color: color of the line. This is specified as a tuple with the 3 colors in the BGR format (assuming that we are working with colored images).
  • thickness: thickness of the line, in pixels.

Note that this function supports other parameters that, when not specified, have default values. Nonetheless, we are not going to make use of them on this tutorial.

We will draw a first line with a blue color (B=255, G=0, R=0) between points (x=20, y=10) and (x=100, y=10) and with a thickness of 2 pixels.

cv2.line(image, (20,10), (100,10), (255,0,0), 2)

Additionally, we are also going to draw a diagonal line from the top left corner to the bottom right corner. In terms of coordinates, it means the line will start on point (x=0, y=0) and end on point (x=width, y=height). We are going to draw this line in red (B=0, G=0, R=255) and with a thickness of 12 pixels.

cv2.line(image, (0,0), (width, height), (0,0,255), 12)

To finalize, we are going to display the image with the drawn lines.

cv2.imshow("Image", image) cv2.waitKey(0) cv2.destroyAllWindows()

The final code can be seen below.

import cv2 image = cv2.imread('C:/Users/N/Desktop/test.jpg') height = image.shape[0] width = image.shape[1] cv2.line(image, (20,10), (100,10), (255,0,0), 2) cv2.line(image, (0,0), (width, height), (0,0,255), 12) cv2.imshow("Image", image) cv2.waitKey(0) cv2.destroyAllWindows()

Testing the code

To test the code, simply run it using a tool of your choice. In my case I’ve used IDLE, a Python IDE.

The image used for testing is the one from figure 1.

Original image used in the test.

After running the code, you should get a result similar to figure 2. Naturally you can use any image of your choice.

Источник

Drawing Lines using Tkinter – Basics for Beginners

Drawing Different Types Of Lines In Tkinter

Tkinter, an in-built Python library, excels in making complex GUIs comprehensible and line drawing accessible. This guide will demystify the process, teaching you how to draw straight lines, plot dotted lines, and even design elaborate shapes using multiple lines, all with the help of Tkinter’s canvas class. Let’s get started!

In Python’s Tkinter, lines are drawn using the create_line() method of the Canvas class. This method takes coordinates to determine line placement, length, and orientation. Parameters like width and dash allow customization of line appearance

Setting up the Drawing Board: Tkinter and Canvas Initialization

Let’s begin by importing the required libraries and setting the basic window up. This will serve as a space for the below demonstrations.

from tkinter import * root = Tk() canvas = Canvas(root) root.geometry("500x500") root.mainloop()

Mastering Lines in Tkinter: A Handy Guide

For creating lines on our main Tkinter window we’ll use the create_line() method which takes the coordinates for line placement on the window. These coordinates decide the length and orientation of the line.

1. Straight Line

Creating any type of line is pretty easy in Tkinter. For drawing a straight we’ll use the create_line() method.

canvas.create_line(15, 25, 200, 25, width=5) canvas.pack()

Straight Line In Tkinter

2. Dotted Line

The procedure of creating a dotted line is the same as the straight line. Similarly, we’ll use the create_line() method and pass the line coordinate, the only change is that we’ll also add another parameter dash.

canvas.create_line(300, 35, 300, 200, dash=(10), width=5) canvas.pack()

Dotted Line In Tkinter

3. Drawing Shapes With Multiple Lines

As we’ve discussed, we can also control the orientation of the lines which enables us to draw different shapes by creating multiple lines. In the given code we’ve taken 3 coordinates of three lines in such a way that it forms a triangle.

canvas.create_line(55, 85, 155, 85, 105, 180, 55, 85, width=5) canvas.pack()

Shapes Using Lines In Tkinter

In the given code, we’ve drawn a triangle by specifying three coordinate pairs for the vertices. The line drawn connects these points in the order given, creating a triangle

Taking Your First Step in Tkinter Drawing: Wrapping Up

And voilà! You’ve taken your first steps in mastering line drawing in Tkinter. As you explore the create_line() function and its parameters, your canvas will only get more lively. What will you create next with your newfound skills?

Источник

Читайте также:  Php читаем файл txt
Оцените статью