# 基于python的两张图片RGBA alpha 透明度混合实现

网上没有找到现成的python代码实现，只能看c++的实现代码并用python做一个简单实现。

scr_channels = cv2.split(co)
dstt_channels = cv2.split(img)
b, g, r, a = cv2.split(co)
for i in range(3):
dstt_channels[i][100:200,300:400] = dstt_channels[i][100:200,300:400]*(255.0-a)/255
dstt_channels[i][100:200,300:400] += np.array(scr_channels[i]*(a/255), dtype=np.uint8)
cv2.imwrite("img_target.png", cv2.merge(dstt_channels))

h, w, c = image.shape
#animal angumention
# cv2.imwrite('1_result.png', cv2.flip(img,-1))
# cv2.imwrite('1_result.png', cv2.resize(img, None,fx=0.2,fy=2, interpolation = cv2.INTER_CUBIC))
if cfg.animal_augment and random.uniform(0, 1) >= 0.4:
animal_sample = []
choices = random.sample(self.animal_aug_list, random.randint(1,4))
# print(choices)
for e in choices:
animal_sample.append(os.path.join(cfg.animal_aug_dir, e))
for i_idx, img_path in enumerate(animal_sample):
# print(img_path)
# if len(i) != 4:
#     continue
if random.uniform(0, 1) > 0.5:
i = cv2.flip(i, random.choice([0, 1, -1]))
if random.uniform(0, 1) > 0.5:
coor_rate = round(random.uniform(0.5, 1.2), 1)
i = cv2.resize(i, None, fx=coor_rate, fy=coor_rate, interpolation = cv2.INTER_CUBIC)
i_h, i_w, i_c = i.shape
dstt_channels = cv2.split(image)
scr_channels = cv2.split(i)
if len(scr_channels) != 4:
# print("pass")
continue
b, g, r, a = cv2.split(i)
# print(i.shape)
if random.uniform(0, 1) >= 0.5:
coor_xmin = random.randint(5, w//2-i_w)
else:
coor_xmin = random.randint(w//2+i_w, w-i_w)
coor_ymin = random.randint(10, h-i_h)
for j in range(3):
dstt_channels[j][coor_ymin:(coor_ymin+i_h), coor_xmin:(coor_xmin+i_w)] = dstt_channels[j][coor_ymin:(coor_ymin+i_h), coor_xmin:(coor_xmin+i_w)]*(255.0-a)/255
dstt_channels[j][coor_ymin:(coor_ymin+i_h), coor_xmin:(coor_xmin+i_w)] += np.array(scr_channels[j]*(a/255), dtype=np.uint8)
image = cv2.merge(dstt_channels)