文本变成图像总是灰色的

2024-09-27 00:16:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把一个透明的背景图片和文字的颜色。我把文字放进图像,但字体颜色总是灰色的

# -*- 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)



Tags: thealphalayerimgpng颜色cv2dst
1条回答
网友
1楼 · 发布于 2024-09-27 00:16:08

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

fontColor              = [255, 0, 0, 255]

相关问题 更多 >

    热门问题