python随机把图像填充最暗颜色

import os

import cv2
import numpy as np
import random


if __name__ == '__main__':

    dir_a=r'E:\lian'

    debug_show=False

    mask_paths = ['%s/%s' % (i[0].replace("\\", "/"), j) for i in os.walk(dir_a) for j in i[-1] if j.endswith((f'_mask.jpg', 'jpega', 'JPGa'))]
    mask_paths = sorted(mask_paths, key=lambda x: os.path.getmtime(x))
    for index, mask_path in enumerate(mask_paths):
        img_path=mask_path.replace('_mask.jpg','_img.jpg')
        img_o = cv2.imread(img_path)
        img=img_o.copy()

        mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)

        # 在二值图像中找到白色区域的位置
        white_pixels = np.where(mask == 255)

        # 计算白色区域的面积
        white_area = len(white_pixels[0])

        # 确定新区域的面积,小于白色区域的90%
        new_area = int(white_area * 0.9)

        # 生成随机形状和大小的结

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/130893785
今日推荐