OpenCV-Python是旨在解決計算機視覺問題的Python綁定庫。cv2.putText()
方法用於在任何圖像上繪製文本字符串。
用法: cv2.putText(image, text, org, font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
參數:
image:它是要在其上繪製文本的圖像。
text:要繪製的文本字符串。
org:它是圖像中文本字符串左下角的坐標。坐標表示為兩個值的元組,即(X坐標值,Y坐標值)。
font:它表示字體類型。一些字體類型是FONT_HERSHEY_SIMPLEX,FONT_HERSHEY_PLAIN,等
fontScale:字體比例因子乘以font-specific基本大小。
color:它是要繪製的文本字符串的顏色。對於BGR,我們通過一個元組。例如:(255,0,0)為藍色。
thickness:它是線的粗細像素。
lineType:這是一個可選參數,它給出了要使用的行的類型。
bottomLeftOrigin:這是一個可選參數。如果為true,則圖像數據原點位於左下角。否則,它位於左上角。
返回值:它返回一個圖像。
用於以下所有示例的圖像:
範例1:
# Python program to explain cv2.putText() method
# importing cv2
import cv2
# path
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
# Reading an image in default mode
image = cv2.imread(path)
# Window name in which image is displayed
window_name = 'Image'
# font
font = cv2.FONT_HERSHEY_SIMPLEX
# org
org = (50, 50)
# fontScale
fontScale = 1
# Blue color in BGR
color = (255, 0, 0)
# Line thickness of 2 px
thickness = 2
# Using cv2.putText() method
image = cv2.putText(image, 'OpenCV', org, font,
fontScale, color, thickness, cv2.LINE_AA)
# Displaying the image
cv2.imshow(window_name, image)
範例2:
# Python program to explain cv2.putText() method
# importing cv2
import cv2
# path
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
# Reading an image in default mode
image = cv2.imread(path)
# Window name in which image is displayed
window_name = 'Image'
# text
text = 'GeeksforGeeks'
# font
font = cv2.FONT_HERSHEY_SIMPLEX
# org
org = (00, 185)
# fontScale
fontScale = 1
# Red color in BGR
color = (0, 0, 255)
# Line thickness of 2 px
thickness = 2
# Using cv2.putText() method
image = cv2.putText(image, text, org, font, fontScale,
color, thickness, cv2.LINE_AA, False)
# Using cv2.putText() method
image = cv2.putText(image, text, org, font, fontScale,
color, thickness, cv2.LINE_AA, True)
# Displaying the image
cv2.imshow(window_name, image)
輸出:
相關用法
- Python OpenCV cv2.circle()用法及代碼示例
- Python OpenCV cv2.imread()用法及代碼示例
- Python OpenCV cv2.imwrite()用法及代碼示例
- Python OpenCV cv2.line()用法及代碼示例
- Python OpenCV cv2.rectangle()用法及代碼示例
- Python OpenCV cv2.ellipse()用法及代碼示例
- Python OpenCV cv2.arrowedLine()用法及代碼示例
- Python OpenCV cv2.blur()用法及代碼示例
- Python OpenCV cv2.erode()用法及代碼示例
- Python OpenCV cv2.cvtColor()用法及代碼示例
- Python OpenCV cv2.copyMakeBorder()用法及代碼示例
- Python OpenCV cv2.imshow()用法及代碼示例
注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 Python OpenCV | cv2.putText() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。