【发布时间】:2021-01-05 15:37:49
【问题描述】:
我正在尝试使用此功能在透明徽标上应用叠加层:
def overlay(path):
logo_img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
'''Saving the alpha channel of the logo to the "alpha" variable.'''
alpha = logo_img[:, :, 3]
'''Creating the overlay of the logo by first creating an array of zeros in the shape of the logo.
The color on this will change later to become the overlay that will mask the logo.'''
mask = np.zeros(logo_img.shape, dtype=np.uint8)
'''Adding the alpha (transparency) of the original logo so that the overlay has the same transparecy
that the original logo has.'''
# mask[:, :, 2] = alpha
'''This code chooses random values for Red, Green and Blue channels of the image so that the final
overlay always has a different background'''
# r, g, b = (random.randint(0, 255),
# random.randint(0, 255),
# random.randint(0, 255))
r, g, b = (0, 255, 0)
'''There is a risk of losing the transparency when randomizing the overlay color so here I'm saving the
alpha value'''
a = 255
'''Creating the overlay'''
mask[:, :] = r, g, b, a
mask[:, :, 3] = alpha
'''Alp, short for alpha, is separate from above. This determines the opacity level of the logo. The
beta parameter determines the opacity level of the overlay.'''
alp = 1
beta = 1 - alp
'''addWeighted() is what masks the overlay on top of the logo'''
dst = cv2.addWeighted(logo_img, alp, mask, beta, 0, dtype=cv2.CV_32F).astype(np.uint8)
'''Converting the output dst to a PIL image with the RGBA channels.'''
pil_image = Image.fromarray(dst).convert('RGBA')
return pil_image
如您所见,我有这两个元组用于设置 RGB。无论是随机化还是选择特定颜色,叠加层的颜色都没有区别。
# r, g, b = (random.randint(0, 255),
# random.randint(0, 255),
# random.randint(0, 255))
r, g, b = (0, 255, 0)
【问题讨论】:
标签: python numpy opencv python-imaging-library rgb