Draw text on image
1 Overview
In OpenCV, calling the cv2.putText() function can add text to a specified location, which provides a more direct and convenient way for scenes where text needs to be added to pictures.
Note: OpenCV does not support displaying Chinese characters, and the text string added when using cv2.putText() cannot contain Chinese characters (including Chinese punctuation marks)
2. cv2.putText() function parameters
Its function prototype is as follows:
cv2.putText(img, text, org, fontFace, fontScale, color, thickness=None, lineType=None, bottomLeftOrigin=None)
The above parameters respectively represent:
-
img: image that needs to draw text;
-
text: the text content to be drawn;
-
org: Indicates the position to be drawn, the lower left corner of the text string in the image; a tuple can be used to represent the x and y coordinates, for example (10, 100) means x=10, y=100.
-
fontFace: font type, such as cv2.FONT_HERSHEY_SIMPLEX, cv2.FONT_HERSHEY_PLAIN, etc.; the corresponding font types are as follows:
cv2.FONT_ITALIC:斜体字的标志 cv2.FONT_HERSHEY_PLAIN:小尺寸无衬线字体 cv2.FONT_HERSHEY_SIMPLEX:正常大小的无衬线字体 cv2.FONT_HERSHEY_DUPLEX:正常大小的无衬线字体(比FONT_HERSHEY_SIMPLEX更复杂) cv2.FONT_HERSHEY_COMPLEX:正常大小的衬线字体 cv2.FONT_HERSHEY_TRIPLEX:正常大小的衬线字体(比FONT_HERSHEY_COMPLEX更复杂) cv2.FONT_HERSHEY_SCRIPT_SIMPLEX:手写体字体 cv2.FONT_HERSHEY_SCRIPT_COMPLEX(比FONT_HERSHEY_SCRIPT_SIMPLEX的更复杂)
-
fontScale: The size of the font, the font scale factor multiplied by the font-specific base size;
-
color: text font color, set the three-channel tuple BGR, such as (255, 0, 0)
常见颜色:
red (0, 0, 255)
green (0, 128, 0)
blue (255, 0, 0)
yellow (0, 255, 255)
purple (128, 0, 128)
orange (0, 165, 255)
white (255, 255, 255)
black (0, 0, 0)
gray (128, 128, 128) -
thickness: font thickness, default is 1;
-
lineType: line type, default is cv2.LINE_AA;
-
bottomLeftOrigin: coordinate origin. If true, the image data origin is in the lower left corner, otherwise it is in the upper left corner;
Among them, org defines the starting position of the text, which can be used Yuanzu represents the x and y coordinates, for example (10, 100) represents x=10, y=100.
3. Draw non-Chinese text on the image
import cv2
img = cv2.imread('test.png') # 读取彩色图像(BGR)
cv2.putText(img, '@Elaine', (300, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (128, 128, 128), 2, cv2.LINE_AA)
cv2.imshow('test', img) # 显示叠加图像
cv2.waitKey() # 等待按键命令
Show results:
4. Draw Chinese text on the image
(1) If no processing is performed and cv2.putText() is used to display directly, what will the Chinese text become? , OpenCV does not support displaying Chinese characters.
import cv2
img = cv2.imread('test.png') # 读取彩色图像(BGR)
cv2.putText(img, '@Elaine猿', (300, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (128, 128, 128), 2, cv2.LINE_AA)
cv2.imshow('test', img) # 显示叠加图像
cv2.waitKey() # 等待按键命令
Show results:
(2) Adding Chinese characters to the image can be achieved using opencv+PIL
Pay attention to the mutual conversion between Image.fromarray() and np.array()
Reference link: https://blog.csdn.net/qq_41273999/article/details/ 134569426?spm=1001.2014.3001.5501
import cv2
from PIL import Image, ImageDraw, ImageFont
import numpy as np
img = cv2.imread('test.png') # 读取彩色图像(BGR)
imgPIL = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# cv2.putText(img, '@Elaine猿', (300, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (128, 128, 128), 2, cv2.LINE_AA)
drawPIL = ImageDraw.Draw(imgPIL)
textSize = 35
fontText = ImageFont.truetype("font/simsun.ttc", textSize, encoding="utf-8")
pos = (300, 40) # (left, top),字符串左上角坐标
text = '@Elaine猿'
color = (128, 128, 128) # gray
drawPIL.text(pos, text, color, font=fontText)
imgPutText = cv2.cvtColor(np.asarray(imgPIL), cv2.COLOR_RGB2BGR)
cv2.imshow('test', imgPutText) # 显示叠加图像
cv2.waitKey() # 等待按键命令
Show results:
5. Add oblique transparent text watermark to the picture
method 1:
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import cv2
# 读取彩色图像(BGR)
img = cv2.imread('test.png')
imgPIL = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 图片的颜色模式设置为RGBA
img_RGBA = imgPIL.convert('RGBA')
# 新建一个和原图大小一样的水印覆盖层
text_overlay = Image.new('RGBA', img_RGBA.size, (255, 255, 255, 0))
# 创建一个画图对象
drawPIL = ImageDraw.Draw(text_overlay)
# 设置字体大小
textSize = 35
fontText = ImageFont.truetype("font/simsun.ttc", textSize, encoding="utf-8")
# 在指定位置画上文字水印
box = drawPIL.textbbox((0, 0), '@Elaine猿', font=fontText)
text_width, text_height = box[2], box[3]
x = img_RGBA.width / 2 - text_width / 2
y = img_RGBA.height / 2 - text_height / 2
# fill参数的(128, 128, 128)为颜色gray, 最后一位为透明度
drawPIL.text((x, y), '@Elaine猿', font=fontText, fill=(128, 128, 128, 160))
# 设置文本旋转角度
angle = 20
# 中心点
center = (img_RGBA.width / 2, img_RGBA.height / 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
text_overlay = cv2.warpAffine(np.array(text_overlay), M, (img_RGBA.width, img_RGBA.height), flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_REPLICATE)
text_overlay = Image.fromarray(text_overlay)
# 合成透明图像和背景不透明图像
img_RGBA = Image.alpha_composite(img_RGBA, text_overlay)
img_RGB = img_RGBA.convert('RGB')
imgPutText = cv2.cvtColor(np.asarray(img_RGB), cv2.COLOR_RGB2BGR)
cv2.imshow('test', imgPutText) # 显示叠加图像
cv2.waitKey() # 等待按键命令
Effect display:
Method 2:
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import cv2
# 打开图片Image格式
img = Image.open('test.png')
# 图片的颜色模式设置为RGBA
img_RGBA = img.convert('RGBA')
# 新建一个和原图大小一样的水印覆盖层
text_overlay = Image.new('RGBA', img_RGBA.size, (255, 255, 255, 0))
# 创建一个画图对象
image_draw = ImageDraw.Draw(text_overlay)
# 设置字体大小
text_size = 20
font = ImageFont.truetype("font/simsun.ttc", text_size, encoding="utf-8")
# 在指定位置画上文字水印
text = '@Elaine猿'
box = image_draw.textbbox((0, 0), text, font=font)
text_width, text_height = box[2], box[3]
x = img_RGBA.width / 2 - text_width / 2
y = img_RGBA.height / 2 - text_height / 2
# fill参数的(128, 128, 128)为颜色gray, 最后一位为透明度
image_draw.text((x, y), text, font=font, fill=(128, 128, 128, 800))
# 设置文本旋转角度
angle = 30
# 中心点
center = (img_RGBA.width / 2, img_RGBA.height / 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
text_overlay = cv2.warpAffine(np.array(text_overlay), M, (img_RGBA.width, img_RGBA.height), flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_REPLICATE)
text_overlay = Image.fromarray(text_overlay)
# 合成透明图像和背景不透明图像
img_RGBA = Image.alpha_composite(img_RGBA, text_overlay)
img_RGB = img_RGBA.convert('RGB')
img_RGB.show()