Python画像に文字列を描き込み【OpenCV】

コンピュータ

画像上に文字列を描きこむサンプルです。

import cv2
import numpy as np

# 画像に文字列を描きこみ
def write_string(img, str):
    # 四角形(白色で塗りつぶし)
    cv2.rectangle(img, (10, 10), (200, 50), (255,255,255), thickness=-1)
    # 文字列を書き込み
    cv2.putText(img, str, (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,0,0), 2)
    return img


# ファイルの読み込み(Gray)
img = cv2.imread("matu.png", 0)
# 文字列
str = "AI Matu"
dst = write_string(img, str)
# 確認のため画像を表示
cv2.imshow(str, dst)
cv2.waitKey()
cv2.destroyAllWindows()

コメント