Add Python examples to the random drawing tutorial (#31222)

* docs: add Python to the random drawing tutorial

* Match the Python drawing finale to the C++ saturating fade
This commit is contained in:
Viswesh
2026-12-01 15:50:11 +05:30
committed by GitHub
parent 94a5d75358
commit 7b6822aafb
3 changed files with 75 additions and 191 deletions

View File

@@ -13,6 +13,7 @@ import numpy as np
import cv2 as cv
# Drawing Lines
# [lines]
def lines():
for i in range(NUMBER*2):
pt1, pt2 = [], []
@@ -30,6 +31,7 @@ def lines():
cv.imshow(wndname, image)
if cv.waitKey(DELAY)>=0:
return
# [lines]
# Drawing Rectangle
def rectangle():
@@ -145,6 +147,7 @@ def circles():
return
# Draws a text string
# [random_text]
def string():
for i in range(NUMBER):
org = []
@@ -156,17 +159,20 @@ def string():
cv.imshow(wndname, image)
if cv.waitKey(DELAY) >= 0:
return
# [random_text]
# [final_text]
def string1():
textsize = cv.getTextSize("OpenCV forever!", cv.FONT_HERSHEY_COMPLEX, 3, 5)
org = (int((width - textsize[0][0])/2), int((height - textsize[0][1])/2))
for i in range(0, 255, 2):
image2 = np.array(image) - i
image2 = cv.subtract(image, (i, i, i, 0))
cv.putText(image2, "OpenCV forever!", org, cv.FONT_HERSHEY_COMPLEX, 3, (i, i, 255), 5, lineType)
cv.imshow(wndname, image2)
if cv.waitKey(DELAY) >= 0:
return
# [final_text]
if __name__ == '__main__':
print(__doc__)
@@ -176,9 +182,11 @@ if __name__ == '__main__':
width, height = 1000, 700
lineType = cv.LINE_AA # change it to LINE_8 to see non-antialiased graphics
x1, x2, y1, y2 = -width/2, width*3/2, -height/2, height*3/2
# [setup]
image = np.zeros((height, width, 3), dtype = np.uint8)
cv.imshow(wndname, image)
cv.waitKey(DELAY)
# [setup]
lines()
rectangle()
ellipse()