#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import cv2, os, sys

def Stack_pic(Background_path, Overlay_path, Result_dir, alpha=0.3):
    # 读取两张没有alpha通道的图片
    img1 = cv2.imread(Background_path) # 底层图片
    img2 = cv2.imread(Overlay_path) # 顶层图片
    
    Result_name = os.path.splitext(os.path.basename(Background_path))[0]

    # 将img2调整为与img1大小相同
    img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))

    # 将img2的透明度调整为20%
    overlay_alpha = alpha

    # 将img2叠加到img1上
    overlay = cv2.addWeighted(img1, 1 - overlay_alpha, img2, overlay_alpha, 0)

    # 保存结果
    if not os.path.exists(Result_dir):
        os.makedirs(Result_dir)
    cv2.imwrite(os.path.join(Result_dir, Result_name+'.png'), overlay)
    print("堆叠图片写入地址：", os.path.join(Result_dir, Result_name+'.png'))


if __name__ == '__main__':
    Background_path = sys.argv[1] # 背景所在路径
    Overlay_path = sys.argv[2] # 上层图片所在路径
    Result_dir = sys.argv[3] # 结果所在目录
    # 透明度，默认为0.3
    try:
        alpha = float(sys.argv[4])
        if(alpha > 1 or alpha < 0):
            print("alpha 透明度输入不正确，其值应该在0~1之间")
            alpha = 0.3
    except:
        alpha = 0.3
    # 进行对叠程序
    Stack_pic(Background_path, Overlay_path, Result_dir, alpha)