今天我们将使用opencv实现封面图所示的区域透明覆盖,这种透明覆盖在移动app上经常使用。代码如下:
# USAGE
# python overlay.py
# import the necessary packages
from __future__ import print_function
import numpy as np
import cv2
# load the image
image = cv2.imread("kobe.jpg")
# loop over the alpha transparency values
for alpha in np.arange(0, 1.1, 0.1)[::-1]:
# create two copies of the original image -- one for
# the overlay and one for the final output image
overlay = image.copy()
output = image.copy()
# draw a green rectangle surrounding kobe'hand in the image
# along with the text "PyImageSearch" at the top-left
# corner
cv2.rectangle(overlay, (150, 22), (260, 150),
(0, 0, 255), -1)
cv2.putText(overlay, "alpha={}