文字变成图片始终为灰色

时间:2019-06-13 13:36:38

标签: python image opencv

我想在透明背景的图像上放彩色文本。我将文本放入图像中,但字体颜色始终为灰色

# -*- coding: utf-8 -*-
import cv2

img = cv2.imread('Base-0.png', cv2.IMREAD_UNCHANGED)

# Save the transparency channel alpha


font                   = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = (300,500)
fontScale              = 1
fontColor              = [255, 0, 0]
lineType               = 1



gray_layer = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Duplicate the grayscale image to mimic the BGR image and finally add the transparency
*_, alpha = cv2.split(img)
dst = cv2.merge((gray_layer, gray_layer, gray_layer, alpha))

cv2.putText(dst,'Hello', 
    bottomLeftCornerOfText, 
    font, 
    fontScale,
    fontColor,
    lineType)

hsv=cv2.cvtColor(dst,0)
cv2.imwrite("result.png", hsv)


1 个答案:

答案 0 :(得分:0)

您正在为字体使用RGB颜色,并将其应用于RGBA色彩空间。我猜想cv2假设第四个(alpha)值因此为0,从而使您的文本透明。将您的字体颜色更新为:

fontColor              = [255, 0, 0, 255]