77# 配置日志
88logging .basicConfig (level = logging .INFO , format = '%(asctime)s - %(levelname)s - %(message)s' )
99
10- def Image_Merge (left_image_path , right_image_path , is_save = True ):
10+ # 以脚本所在目录为基准,定位到主题根目录下的 image-material
11+ _ROOT = os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))
12+ _MATERIAL = os .path .join (_ROOT , 'image-material' )
13+
14+
15+ def Image_Merge (left_image_path , right_image_path , output_path , is_save = True ):
1116 try :
1217 # 打开两张图片,并转成RGBA格式,用于调整透明图层
1318 logging .info (f"正在打开图片: { right_image_path } 和 { left_image_path } " )
@@ -26,10 +31,10 @@ def Image_Merge(left_image_path, right_image_path, is_save=True):
2631
2732 w , h = image1 .size
2833
29- # 对vi图片作为背景 ,创建一个绘图对象
34+ # 对 vi 图片作为背景 ,创建一个绘图对象
3035 draw = ImageDraw .Draw (image1 )
3136
32- # 绘制一个斜对角线的三角形,即左上角的三角形,设置透明度为0
37+ # 绘制一个斜对角线的三角形,即左上角的三角形,设置透明度为 0
3338 logging .info ("正在绘制透明三角形..." )
3439 draw .polygon ([(0 , 0 ), (w , 0 ), (0 , h )], fill = (0 , 0 , 0 , 0 ))
3540
@@ -42,8 +47,6 @@ def Image_Merge(left_image_path, right_image_path, is_save=True):
4247 # draw.line([w, 0, 0, h], fill="white", width=3)
4348
4449 if is_save :
45- # 保存图片为PNG格式
46- output_path = r'D:\Admin\Downloads\result.png'
4750 logging .info (f"正在保存图片到: { output_path } " )
4851 result .save (output_path )
4952 logging .info (f"图片保存成功!路径: { os .path .abspath (output_path )} " )
@@ -57,7 +60,8 @@ def Image_Merge(left_image_path, right_image_path, is_save=True):
5760 input ("按 Enter 键退出..." )
5861
5962
60- # 两张图片的路径
61- right_image_path = r'D:\Admin\Downloads\暗黑主题.jpg' # 右下角
62- left_image_path = r'D:\Admin\Downloads\明亮主题.jpg' # 左上角
63- Image_Merge (left_image_path , right_image_path , is_save = True )
63+ # 左上角为明亮主题,右下角为暗黑主题,输出到相同目录
64+ left_image_path = os .path .join (_MATERIAL , 'preview-light.png' )
65+ right_image_path = os .path .join (_MATERIAL , 'preview-dark.png' )
66+ output_path = os .path .join (_MATERIAL , 'preview.png' )
67+ Image_Merge (left_image_path , right_image_path , output_path , is_save = True )
0 commit comments