cv2.line() method - Python OpenCV

Last Updated : 23 Jan, 2026

The cv2.line() method in OpenCV is used to draw straight lines on images or blank canvases. It allows you to specify the start point, end point, color, and thickness, making it useful for annotations, shapes and custom graphics.

Note: For this article we will use a sample image "logo.png", to download click here and make sure to keep the image in same folder as your python script.

Example: This simple example draws a blue line on an image loaded from disk.

Python
import cv2

img = cv2.imread("logo.png")
img = cv2.line(img, (50, 50), (250, 50), (255, 0, 0), 3)

cv2.imshow("Output", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Output
A horizontal blue line appears at the top of the image.

Explanation: cv2.line(img, (50, 50), (250, 50), (255, 0, 0), 3) draws a line from (50, 50) to (250, 50) in blue with thickness 3.

Syntax

cv2.line(image, start_point, end_point, color, thickness)

Parameters:

  • image: Image on which the line will be drawn.
  • start_point: Starting coordinate (x, y).
  • end_point: Ending coordinate (x, y).
  • color: Line color in BGR format (e.g., (0, 255, 0) for green).
  • thickness: Line thickness in pixels.

Examples

Example 1: This example draws a vertical red line on a loaded image.

Python
import cv2

img = cv2.imread("logo.png")
img = cv2.line(img, (100, 20), (100, 300), (0, 0, 255), 4)

cv2.imshow("Line 1", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Output1
A vertical red line appears on the left side of the image.

Explanation: cv2.line(img, (100, 20), (100, 300), (0, 0, 255), 4) draws a red line from top to bottom.

Example 2: This example draws two lines to form a cross on the image.

Python
import cv2

img = cv2.imread("logo.png")
img = cv2.line(img, (50, 50), (200, 200), (0, 255, 0), 3)
img = cv2.line(img, (200, 50), (50, 200), (0, 255, 0), 3)

cv2.imshow("Cross", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Output2
A green “X” shape is drawn on the image.

Explanation:

  • cv2.line(img, (50, 50), (200, 200), ...) draws first diagonal.
  • cv2.line(img, (200, 50), (50, 200), ...) draws second diagonal.

Example 3: This example creates a black canvas and draws a thick white diagonal line.

Python
import cv2
import numpy as np

img = np.zeros((400, 400, 3), dtype="uint8")
img = cv2.line(img, (50, 50), (350, 350), (255, 255, 255), 8)

cv2.imshow("Canvas Line", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

Output3
A blank black image with a thick white diagonal line.

Explanation:

  • np.zeros((400, 400, 3)) creates a black image.
  • cv2.line(img, (50, 50), (350, 350), (255, 255, 255), 8) draws a white diagonal.
Comment

Explore