Introduction
Drawing on images helps highlight or mark important parts, making it easier to understand or explain what the image shows.
Jump into concepts and practice - no test required
cv2.line(image, start_point, end_point, color, thickness) cv2.rectangle(image, top_left_corner, bottom_right_corner, color, thickness) cv2.circle(image, center, radius, color, thickness) cv2.putText(image, text, position, font, font_scale, color, thickness, lineType=cv2.LINE_AA)
cv2.line(img, (10, 10), (100, 10), (255, 0, 0), 2)
cv2.rectangle(img, (50, 50), (150, 150), (0, 255, 0), 3)
cv2.circle(img, (200, 200), 40, (0, 0, 255), -1)
cv2.putText(img, 'Hello', (10, 250), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
import cv2 import numpy as np # Create a black image img = np.zeros((300, 400, 3), dtype=np.uint8) # Draw a blue line cv2.line(img, (50, 50), (350, 50), (255, 0, 0), 3) # Draw a green rectangle cv2.rectangle(img, (50, 100), (350, 200), (0, 255, 0), 4) # Draw a filled red circle cv2.circle(img, (200, 250), 40, (0, 0, 255), -1) # Put white text cv2.putText(img, 'Test Image', (100, 290), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) # Save the image to file cv2.imwrite('output_image.png', img) # Print confirmation print('Drawing complete and image saved as output_image.png')
cv2.putText controls the thickness of the text?cv2.line(img, (10, 10), (100, 10), (0, 0, 255), 2)
cv2.circle(img, (50, 50), -10, (255, 0, 0), 3)
img = cv2.imread('image.jpg')
start = (30, 30)
end = (150, 150)
# Options below